Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB GUI Interface to run VBS Script

I have a VBS script which I need to run on a monthly basis which captures file information such as file Name, Type, Date Modified and more. When I processes each file it saves it all onto a CSV file so that I can process it on Excel.

To run the script I setup a batch file .bat

The issue is I need a GUI interface of some sort so that when the batch or vbs file is run it will ask the user to enter a drive letter to scan.

This is the code I have:

test.vbs:
Option Explicit
Dim objFS, objFld
Dim objArgs
Dim strFolder, strDestFile, blnRecursiveSearch
Dim strLines()
Dim i
Dim strCsv

    i = 0
    
'   'Get the commandline parameters
'   Set objArgs = WScript.Arguments 
'   strFolder = objArgs(0)
'   strDestFile = objArgs(1)
'   blnRecursiveSearch = objArgs(2)
    
    '###################################
    'MAKE SURE THESE VALUES ARE CORRECT
    '###################################
    strFolder = "C:\" 
    strDestFile = "C:\test\Output.csv" 
    blnRecursiveSearch = True
    
    'Create the FileSystemObject
    Set objFS=CreateObject("Scripting.FileSystemObject")
    'Get the directory you are working in 
    Set objFld = objFS.GetFolder(strFolder)
    
    'Now get the file details
    GetFileDetails objFld, blnRecursiveSearch 
    
    'Write the csv file
    Set strCsv = objFS.CreateTextFile(strDestFile, True)
    strCsv.Write Join(strLines, vbCrLf)
    
    'Close and cleanup objects
    strCsv.Close
    Set strCsv = Nothing
    Set objFld = Nothing
    Set strFolder = Nothing
    Set objArgs = Nothing

    
Private Sub GetFileDetails(fold, blnRecursive)
Dim fld, fil
dim strLine(5)

    If blnRecursive Then
        'Work through all the folders and subfolders
        For Each fld In fold.SubFolders
            GetFileDetails fld, True 
        Next
    End If
    
    'Now work on the files
    For Each fil in fold.Files
        strLine(0) = fil.Path
        strLine(1) = fil.Type
        strLine(2) = fil.Size
        strLine(3) = fil.DateCreated
        strLine(4) = fil.DateLastModified
        strLine(5) = fil.DateLastAccessed
        
        Redim Preserve strLines(i)
        strLines(i) = Join(strLine, ",")
        i = i + 1
    Next
end sub

And the run.bat

cscript.exe C:\script\test.vbs

As you can see test.vbs specifies what section to scan and capture. code: strFolder = "C:\"

What would be your best recommendation, the people running this are a lot less experienced then me with VB, so they will need some sort of GUI interface that will ask for a drive letter input and then modify line code strFolder = "C:\" to whatever drive letter they entered and then run test.vbs.

like image 762
MalsiaPro Avatar asked Dec 22 '22 05:12

MalsiaPro


2 Answers

It would be easiest just to add the drive letter as a parameter of your vbscript.

Set oArgs = WScript.Arguments
DriveLetter = oArgs(0)
strFolder = DriveLetter & ":\"

Then you can just run the script like you were with the drive letter appended.

cscript.exe C:\script\test.vbs C

You can then wrap the script in VB GUI (input box as suggested before) if the users really need it. Or better yet your script could just ask them to type in the drive letter.

Side note (depending on which version of windows you are using and how you need the dates), the dir command will print a specific file date using the /t switch. So dir /ta would print the last accessed date. Unfortunately it only does one (accessed, modified, created) at a time. You might be able to use that and pipe it to a file (dir /ta > Output.txt) instead of writing a separate script.

like image 75
ktharsis Avatar answered Dec 24 '22 19:12

ktharsis


InputBox is the simplest solution, although you'll probably have to do some error checking.

There's also BrowseForFolder, which lets the users select a folder. Again, you'll probably have to do some error checking if you want to restrict users to just the root folder of a drive.

http://msdn.microsoft.com/en-us/library/bb774065(VS.85).aspx

like image 35
JohnK813 Avatar answered Dec 24 '22 18:12

JohnK813