views:

367

answers:

4

Hi Folks

I have a VBS script which I need to run on a monthly basis which captures file information suhc 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 alot more 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.

Any help is appreciated.

:) Cheers

+1  A: 

You could use an InputBox

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

Booji Boy
sounds easy but to me unfortunately not so, could you incorparate that with my code.Either something that creates a costum batch file or a gui that modifies test.vbs and runs it.Thanks
MalsiaPro
@MalsiaPro, so you need an GUI to update the vbs file with the correct code for the user input path?
o.k.w
Yes o.k.w thats exactly what I need. I cannot fit in the other recommendations with my code, please help if possible.Cheers
MalsiaPro
+1  A: 

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

JohnK813
+1  A: 

why dont use vb.net?

For your drive letter you could even use a drop down box.

Dim driveLetter as String = combobox.text

If (Directory.Exists(driveLetter)) Then
 strFolder = combobox.text
Else
 msgbox("Drive letter does not exist")
End If
PandaNL
+2  A: 

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.

ktharsis