Oct 26, 2012

Folder Details Generator VB Script.

Want to see Folder Structure at a browse of locating the folder. The following  Visual Basic Script generates the Folder details and Stores as a CSV file in the location where you Browsed the Folder. To Use this follow these simple steps.
Steps to be followed:
1) Open a .txt file and copy the following code.

'-------------------Start of code for Folder Details Generator
Option Explicit
'On Error Resume Next

Dim indentSpace,strPrompt,intOptions,strTargetPath,strFolderPath,objShell,wshShell,objFSO,objNewFile,objFolder
Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H200
Const BIF_RETURNONLYFSDIRS = &H1
Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'**Browse For Folder To Be Processed
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = wshShell.SpecialFolders("MyComputer")
strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)

Set objNewFile = objFSO.CreateTextFile("folderDetails.csv", True)
Set objFolder = objFSO.GetFolder(strFolderPath)
' CHANGE STARTS HERE :

Call traverseFolders(strFolderPath, objNewFile, "")


objNewFile.Close
Wscript.Echo "Operation Completed" & Chr(10) & "A file called folderDetails.csv has been generated in the folder from where this script was run."

'routine to count number of files or subfolders
Sub countNumOfSubObjects (fPath , objType , ByRef objCount )

    Dim currFSObj, currFolder, currSubFolders, subFolder
    
    Set currFSObj = CreateObject("Scripting.FileSystemObject")
    Set currFolder = currFSObj.GetFolder(fPath)
    
    Set currSubFolders = currFolder.SubFolders
    
    If objType = "FILE" Then
        objCount = objCount + currFolder.Files.Count
    ElseIf objType = "FOLDER" Then
        objCount = objCount + currSubFolders.Count
    End If
    
    If Not (currSubFolders Is Nothing) Then
        For Each subFolder In currSubFolders
            countNumOfSubObjects subFolder.Path, objType, objCount
        Next
    End If

End Sub


'Traverse routine
Sub traverseFolders(fPath, objNewFile, indent)
    Dim totalSubFolders , totalSubFiles 
    Dim currFSO, currFolder, currSubFolders, currFiles, lname, folder
    Set currFSO = CreateObject("Scripting.FileSystemObject")
    Set currFolder = currFSO.GetFolder(fPath)
    'get subfolders in current folder.
    Set currSubFolders = currFolder.SubFolders
    'Wscript.Echo "In Folder " + currFolder.Name
    
    objNewFile.WriteLine ("Folder path: " + currFolder.Path)
    objNewFile.WriteLine (indent & "Size of this folder: " & FormatNumber((((currFolder.Size)/1024)/1024),3,-1,0,0) & " MB")
    objNewFile.WriteLine (indent & "Number of subfolders: " & currSubFolders.Count)
    objNewFile.WriteLine (indent & "Number of files: " & currFolder.Files.Count)
    objNewFile.WriteLine ("")
    objNewFile.WriteLine ("Folder Name,Total No. of subfolders,Total No. of files,Size (KB),Size (MB)")
    
    'If this folder has subfolders
    If Not (currSubFolders Is Nothing) Then
        'get the details of all the subfolders
        For Each folder In currSubFolders
            totalSubFolders = 0
            totalSubFiles = 0
            
            countNumOfSubObjects folder.Path, "FILE", totalSubFiles
            countNumOfSubObjects folder.Path, "FOLDER", totalSubFolders
            
            objNewFile.WriteLine (folder.Name & "," & totalSubFolders & "," & totalSubFiles & "," & FormatNumber(((folder.Size)/1024),3,-1,0,0) & "," & FormatNumber(((folder.Size) / 1024)/1024,3,-1,0,0))
        Next
    End If

End Sub

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
    Dim objFolder, objFolderItem

    On Error Resume Next

    Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
    If (objFolder Is Nothing) Then
      Wscript.Quit
    End If
    Set objFolderItem = objFolder.Self
    Browse4Folder = objFolderItem.Path
    Set objFolderItem = Nothing
    Set objFolder = Nothing
End Function
'-------------------End of code for Folder Details Generator
2) Save the txt file with extension ".vbs"
3) Double click the .vbs file
4) It asks you to browse the Folder for which you require Folder details.

5) Browse the folder. Click OK
6) The folder Details will be generated and save s as file in the Folder you Browsed

Isn't that amazing. So start Using. This Helps those who are going with configuration management.

No comments:

Post a Comment