Friday, September 25, 2009

How to Map a Tree Structure

To answer the question I had on how I could map my directory tree structure so I could easily see layout all at once, I turned to an example I found in the book “Windows Server Hacks” by Mitch Tulloch, copyright 2004 O’Reilly & Associates, Inc., ISBN 0-596-00647-0.

Seen below is the snipet of VB code. Simply open Notepad with WordWrap turned off, and paste it in. Save the file as “vbtree.vbs” to whatever point in the directory you wish your mapping to start.

I have slightly modified the introductory comments for clarity.

—————————————————————————————————

‘This script is used to show a simple directory tree
‘In command prompt, navigate to directory folder where this file is stored
‘Type cscript vbtree.vbs to display the tree
‘Type cscript vbtree.vbs > tree.txt to redirect the output to a text file
‘Source: Windows Server Hacks

Option Explicit
Dim sArg, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

‘Get folder (default is current directory)
If Wscript.Arguments.Count > 0 Then
sArg = Wscript.Arguments(0)
Else
sArg = “.”
End If
sArg = oFSO.GetAbsolutePathName(sArg)

‘Process entire tree (if valid folder)
If oFSO.FolderExists(sArg) Then
Wscript.Echo “Folder tree for: “, sArg
ShowTree “", oFSO.GetFolder(sArg)
End If

Set oFSO = Nothing
Wscript.Quit(0)

Sub ShowTree(sIndent, oFolder)
Dim oSubFolder, ix
ix = 1
For Each oSubFolder In oFolder.SubFolders
Wscript.Echo sIndent & “+–” & oSubFolder.Name
If ix <> oFolder.SubFolders.Count Then
ShowTree sIndent & “| “, oSubFolder
Else
ShowTree sIndent & ” “, oSubFolder
End If
ix = ix + 1
Next
End Sub

No comments: