Click here to Skip to main content
15,890,185 members
Articles / Programming Languages / VBScript
Article

VBScript directory crawler

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
16 May 2000 127.1K   2.4K   30   12
A VBScript program that can recursively walk directories and also create a true working tree (good for printouts)
  • Download source files - 1 Kb

    Sample Image - walker.jpg

    This VBScript can be used in a number of ways. At its heart, it's a simple directory tree walker. I wrote it originally as a DevStudio macro to clear out all the big leftover files in the debug and release folders. I added a bit of code to it on a whim to make it to indent the subfolders and add the various lines so that in the end it looks similar to the left hand frame of explorer. This can then be piped to a file and printed (have you ever wanted to print out a directory tree? I know I have.)

    I have commented where you could put code to do things like clear out temp files, whatever.

    Enjoy :)

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    Question23 years later and still valuable. Here's some modernization (works Server 2019, Windows 11, etc.) Pin
    Gabriel Aaron Cunningham17-Mar-23 8:40
    Gabriel Aaron Cunningham17-Mar-23 8:40 
    Option Explicit
    
    ' https://www.codeproject.com/articles/553/vbscript-directory-crawler
    ' Usage:
    '	cscript.exe /nologo Tree.vbs %temp%
    
    
    Call CheckAndReopenConsole()
    
    Dim sTargetFolder : sTargetFolder = "." ' Default to current path
    If WScript.Arguments.Count >= 1 Then sTargetFolder = WScript.Arguments(0)
    
    ' WScript.Echo VbCrLf & "/" & Replace(Space(78), " ", "*") & "\" & VbCrLf
    ' Call GetWorkingFolderRecursion1(sTargetFolder, 0, True, "|", "")
    ' WScript.Echo VbCrLf & "\" & Replace(Space(78), " ", "*") & "/" & VbCrLf
    ' WScript.Echo VbCrLf & VbCrLf
    WScript.Echo VbCrLf & "/" & Replace(Space(78), " ", "*") & "\" & VbCrLf
    Call GetWorkingFolderRecursion2(sTargetFolder, 0, True, "")
    WScript.Echo VbCrLf & "\" & Replace(Space(78), " ", "*") & "/" & VbCrLf
    Call Pause()
    
    
    ' ------------------------------------------------------------------------------
    Sub CheckAndReopenConsole
    	If UCase(Right(WScript.FullName, 12)) <> "\CSCRIPT.EXE" Then
    		Dim sArgs : sArgs = " "
    		Dim sArg, sCMD
    		' WScript.Echo WScript.Arguments.Count
    		If WScript.Arguments.Count > 0 Then
    			For Each sArg in WScript.Arguments
    				sArgs = sArgs & sArg & " "
    			Next
    		End If
    		sCMD = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & sArgs
    		CreateObject("WScript.Shell").Run(sCMD)
    		WScript.Quit 911
    	End If
    End Sub
    ' ------------------------------------------------------------------------------
    Sub Pause()
    	WScript.Echo ("Press Enter to continue")	
    	Do
    	Loop Until WScript.StdIn.Read(1) <> ""
    End Sub
    ' ------------------------------------------------------------------------------
    function GetWorkingFolderRecursion1(sCurrentFolder, iFolderCount, bInitialRun, sSpacer, sLeftIndication)
    	Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
    	Dim oFolder : Set oFolder = oFSO.GetFolder(sCurrentFolder)
    	Dim oSubFolders : Set oSubFolders = oFolder.SubFolders
    	If bInitialRun = True then
    		WScript.Echo oFolder.name
    		sLeftIndication = ""
    		iFolderCount = oSubFolders.count
    		bInitialRun = False
    	End If
    	Dim iRemainingCount : iRemainingCount = oSubFolders.count
    	
    	Dim oSubFolder
    	For Each oSubFolder In oSubFolders
    		sLeftIndication = sLeftIndication & Space(3) & sSpacer
    		WScript.Echo sLeftIndication & "-- " & oSubFolder.name
    		If iRemainingCount = 1 then
    			sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 1)
    			sLeftIndication = sLeftIndication & " "
    		End If
    
    		iRemainingCount = GetWorkingFolderRecursion1 (sCurrentFolder & "\" & oSubFolder.name, iRemainingCount, bInitialRun, sSpacer, sLeftIndication)
    	Next 
    		
    	If Len(sLeftIndication) > 3 then
    		sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 4)
    	End If
    	
    	Set oFSO = nothing
    	GetWorkingFolderRecursion1 = iFolderCount - 1
    End function
    ' ------------------------------------------------------------------------------
    function GetWorkingFolderRecursion2(sCurrentFolder, iFolderCount, bInitialRun, sLeftIndication)
    	' Requires CMD/Terminal/Console to be using a better font such as Lucida Console
    	Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
    	Dim oFolder : Set oFolder = oFSO.GetFolder(sCurrentFolder)
    	Dim oSubFolders : Set oSubFolders = oFolder.SubFolders
    	If bInitialRun = True then
    		WScript.Echo oFolder.name
    		sLeftIndication = ""
    		iFolderCount = oSubFolders.count
    		bInitialRun = False
    	End If
    	Dim iRemainingCount : iRemainingCount = oSubFolders.count
    	Dim sShowRemainingCount : sShowRemainingCount = "" ' " [" & iRemainingCount & "]"
    	Dim oSubFolder
    	For Each oSubFolder In oSubFolders
    		Select Case iRemainingCount
    			Case 2
    				sLeftIndication = Replace(sLeftIndication, ChrW(9500), ChrW(9474)) & Space(3) & ChrW(9500)
    				WScript.Echo sLeftIndication & ChrW(9472) & Chr(32) & oSubFolder.name & sShowRemainingCount
    				sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 1)
    				sLeftIndication = sLeftIndication & ChrW(9500)
    			Case 1
    				sLeftIndication = Replace(sLeftIndication, ChrW(9500), ChrW(9474)) & Space(3) & ChrW(9492)
    				WScript.Echo sLeftIndication & ChrW(9472) & Chr(32) & oSubFolder.name & sShowRemainingCount
    				sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 1)
    				sLeftIndication = sLeftIndication & Chr(183)
    			Case Else
    				sLeftIndication = Replace(sLeftIndication, ChrW(9500), ChrW(9474)) & Space(3) & ChrW(9500)
    				WScript.Echo sLeftIndication & ChrW(9472) & Chr(32) & oSubFolder.name & sShowRemainingCount
    		End Select
    
    		iRemainingCount = GetWorkingFolderRecursion2 (sCurrentFolder & "\" & oSubFolder.name, iRemainingCount, bInitialRun, sLeftIndication)
    	Next 
    		
    	If Len(sLeftIndication) > 3 then
    		sLeftIndication = Left(sLeftIndication, Len(sLeftIndication) - 4)
    	End If
    	
    	Set oFSO = nothing
    	GetWorkingFolderRecursion2 = iFolderCount - 1
    End function

    PraiseWell ... it still works! Pin
    RedDk21-Oct-16 9:11
    RedDk21-Oct-16 9:11 
    GeneralDocument Properties help Pin
    DangerousInkpot26-Jul-05 8:40
    DangerousInkpot26-Jul-05 8:40 
    GeneralRe: Document Properties help Pin
    Mathew Gorge Whiz27-Jul-05 21:28
    Mathew Gorge Whiz27-Jul-05 21:28 
    QuestionWhy it Fails? Pin
    dougcranston27-Feb-03 4:50
    dougcranston27-Feb-03 4:50 
    AnswerRe: Why it Fails? Pin
    Steve Merritt15-Oct-04 8:09
    Steve Merritt15-Oct-04 8:09 
    GeneralRe: Why it Fails? Pin
    dougcranston15-Oct-04 8:18
    dougcranston15-Oct-04 8:18 
    GeneralRe: Why it Fails? Pin
    Anonymous14-Sep-05 14:32
    Anonymous14-Sep-05 14:32 
    GeneralShow pictures from a dir on a html-page Pin
    20-Feb-02 0:43
    suss20-Feb-02 0:43 
    GeneralSearchinf for a particular extension using VBscript Pin
    Gnanavel9-Oct-00 20:42
    Gnanavel9-Oct-00 20:42 
    GeneralRe: Searchinf for a particular extension using VBscript Pin
    24-Nov-00 8:21
    suss24-Nov-00 8:21 
    GeneralNice! + More on scripts Pin
    Philippe Lhoste23-May-00 22:59
    Philippe Lhoste23-May-00 22:59 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.