| < Day Day Up > |
Hack 49 Determine Who Has A Particular File
|
| < Day Day Up > |
| < Day Day Up > |
Hack 50 Display a Directory Tree
Using some simple coding, you can display a complete map of a directory structure from a command prompt .
The Explorer interface makes it easy to browse directories on a Windows machine, but it doesn't provide a simple method to document the structure of directories and their subdirectories. For troubleshooting purposes, it's helpful to know the directory structure on file servers where users store their work. This VBScript
The CodeType the following code into Notepad (with Word Wrap turned off) and save the file with a .vbs extension as vbtree.vbs :
' Show simple directory tree
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
Running the Hack
The script is hardcoded by design to display the structure of the current directory. Place the script into to directory whose structure you want to display, such as
C:\data
. Then,
Figure 5-5. Displaying the tree of subdirectories under C:\data
Make sure you have the latest scripting engines on the workstation from which you run this script. You can download current scripting engines from the Microsoft Scripting home page (http://msdn.microsoft.com/scripting/). Rod Trent |
| < Day Day Up > |