Up a Tree

MPj04372960000[1] One of the first CMD commands I fell in love with (yes I’m that geeky) was TREE. When executed from a command prompt it would give you a nice graphical representation of the directory structure. Never being satisfied I wrote a VBScript version years ago.  I decided to dust it off and tweak it some. Here is TREE.VBS.

On Error Resume Next
 
If WScript.Arguments.Named.Exists("Path") Then
    strFolder=WScript.Arguments.Named.Item("Path")
Else
    Set objShell = CreateObject("WScript.shell")
    strTemp = objShell.ExpandEnvironmentStrings("%TEMP%")
    strFolder=strTemp
End If
 
nTabLevel=0
 
Set objFSO=CreateObject("Scripting.FileSystemObject")
 
if objFSO.FolderExists(strFolder) then
 
    Set objFolder=objFSO.GetFolder(strFolder)
    WScript.Echo objFolder & " (" & objFolder.Files.count &_
 " files " & FormatNumber(objFolder.Size/1024,2) & " KB)"
 
    Call ProcessSubFolders(nTabLevel,objFolder)
 
else
    wscript.echo "Failed to find " & strFolder
end if
 
'END MAIN SCRIPT
 
 
Sub ProcessSubFolders(nTabLevel,objFolder)
On Error Resume Next
   nTabLevel = nTabLevel +1
 
    Set colSubs=objFolder.SubFolders
    For Each folder In colSubs
      Set colFiles=folder.files
 
       WScript.Echo String(nTabLevel," ") & "|" &_
       String(nTabLevel,"_") & folder.name &_
       " (" & colFiles.count & " files " &_
       FormatNumber(folder.size/1024,2) & " KB)"
     
       'recurse through subfolders 
       ProcessSubFolders ntabLevel,folder
    Next
    
    nTabLevel=nTabLevel-1
End Sub
 

You must run this with CSCRIPT from the command prompt. The usage syntax is pretty simple:

cscript tree.vbs /path:<somefolder>

The default path is %TEMP%. I use the Wscript.Arguments object to pass a runtime parameter and set a default using the Wscript.Shell object’s ExpandEnvironmentStrings() method.

If WScript.Arguments.Named.Exists("Path") Then
    strFolder=WScript.Arguments.Named.Item("Path")
Else
    Set objShell = CreateObject("WScript.shell")
    strTemp = objShell.ExpandEnvironmentStrings("%TEMP%")
    strFolder=strTemp
End If

The script uses the FileSystemObject to retrieve the folder, if it exists and displays the folder name, the total number of files in the immediate folder and the folder size.

Set objFSO=CreateObject("Scripting.FileSystemObject")
 
if objFSO.FolderExists(strFolder) then
 
    Set objFolder=objFSO.GetFolder(strFolder)
    WScript.Echo objFolder & " (" & objFolder.Files.count &_
 " files " & FormatNumber(objFolder.Size/1024,2) & " KB)"

The folder size is formatted in KB. Now the fun part. I have a subroutine that takes a level indicator and a path as parameters.

nTabLevel=0
Call ProcessSubFolders(nTabLevel,objFolder)

The subroutine increments the tab level by one and then returns a collection of subfolders.

Sub ProcessSubFolders(nTabLevel,objFolder)
On Error Resume Next
   nTabLevel = nTabLevel +1
 
    Set colSubs=objFolder.SubFolders
    For Each folder In colSubs
      Set colFiles=folder.files

For each subfolder I create a tree indicator using something like |_____ before the folder name. The length of the underscore and indentation is determined by the level the script is processing.

WScript.Echo String(nTabLevel," ") & "|" &_
  String(nTabLevel,"_") & folder.name &_
  " (" & colFiles.count & " files " &_
  FormatNumber(folder.size/1024,2) & " KB)"

The display includes the foldername, how many files it has and total size. These values are only for the files immediately within the folder. To keep going I recursively call the function for any subfolders.

'recurse through subfolders 
 ProcessSubFolders ntabLevel,folder
next

After processing any subfolders, the level counter is decremented by 1.

nTabLevel=nTabLevel-1

Running the script gives output like this:

C:\test (188 files 15,609.59 KB)
|_foo2 (2 files 812.00 KB)
|_lsi2k8 (12 files 576.01 KB)
|_polymon (2 files 2,336.00 KB)

The script will ignore any folders it can’t access. Vista is notoriously difficult for this sort of thing with all of its junction points so don’t take this output as 100% accurate. Still, I think is fun. Did I mention I’m kinda geeky?

Download the script here.