Wednesday, August 18, 2010

VB Script - List out all the files in a folder and its sub-folders (Recursive function)

VB Script - List out all the files in a folder and its sub-folders (Recursive function)


Following VB Script program will list all the files(Including sub folders) in a selected folder. It would create a text file "AllFilesList" in the selected folder which contain folder path followed by list of files.

Attaching VB script code and generated text file screen shot.

(To execute the program, copy the code and save as .vbs)

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Automation FrameWork\" 'Change the folder path as per your requirement.
Set oNotepad = objFSO.createtextfile(objStartFolder & "AllFilesList.txt") ' Output file created in the above pointed directory.
Set objFolder = objFSO.GetFolder(objStartFolder)
'Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    'Wscript.Echo objFile.Name
    oNotepad.writeline(objFile.Name)
    oNotepad.writeline()
Next
'Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)
oNotepad.Close
Wscript.Echo "Saved Successfully"

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        'Wscript.Echo Subfolder.Path
        oNotepad.writeline(Subfolder.Path)
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            'Wscript.Echo objFile.Name
            oNotepad.writeline(objFile.Name)
        Next
        'Wscript.Echo
        oNotepad.writeline("*********************")
        ShowSubFolders Subfolder
    Next
End Sub 





---

No comments:

Post a Comment