Thursday, December 10, 2009

Get File List using VBS

Save the following code in a text file and "Save As" in '.vbs' extension. Then double click on the .vbs file and provide the folder path for which you want to get report.

'***Code Starts Here+++
Dim Input
Set objFSO = CreateObject("Scripting.FileSystemObject")
Input = InputBox ("Enter the full path to the folder (Defalut Current Directory)","Enter Full Path")

Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
If Trim(Input)="" Then
    Input = "."
End If
Set oFolder = oFS.GetFolder(Input)

strFile = "Report.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFile)
objFile.WriteLine("File Name" & "," & "File Size (in MB)" & "," & "File Path")
ShowMainFolderDetails oFolder
ShowFolderDetails oFolder
msgbox ("Done !!!")

sub ShowFolderDetails(oF)
    dim F
    for each F in oF.Subfolders
        ShowFolderDetails(F)
        ShowFileList(F)
    next
end sub

sub ShowMainFolderDetails(oF)
    ShowFileList(oF)
end sub

Function ShowFileList(fs)
    Set fc = fs.Files
    For Each f1 in fc
        objFile.WriteLine(f1.name & "," & (f1.Size/1024)/1024 & "," & f1.Path)
    Next
    ShowFileList = s
End Function

'***Code Ends Here---
The report will be created in the same folder of .vbs file with name "Report.csv'.

Also using the following code ONLY subfolder list can be obtained:

'***Code Starts Here+++
Dim Input
Set objFSO = CreateObject("Scripting.FileSystemObject")
Input = InputBox ("Enter the full path to the folder (Defalut Current Directory)","Enter Full Path")

Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
If Trim(Input)="" Then
    Input = "."
End If
Set oFolder = oFS.GetFolder(Input)

strFile = "Test.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFile)
objFile.WriteLine("Folder Name" & "," & "Folder Size (in MB)" & "," & "Folder Path")
ShowFolderDetails oFolder
msgbox ("Done !!!")

sub ShowFolderDetails(oF)
    dim F
    for each F in oF.Subfolders
        objFile.WriteLine(F.Name & "," & (F.Size/1024)/1024 & "," & F.Path)
    next
end sub

'***Code Ends Here---

No comments:

Post a Comment