Any of the Words
Link to us
Simple Recursive folder name scan
This is a really simple example of a recursive scan of folders and all sub folders that anybody should be able to understand. My eyes glazed over looking at most code examples of this so I wrote this to show how it can be really simply shown.
Dim path, fpath, mbs Private Sub Form_Load() 'recursively scan all folders and sub folders path = "C:\program files" 'put a valid path to a folder between "". fpath = path 'Choose a folder with a few sub folders and follow what is going Call ShowFolderList(path) 'in single step mode. MsgBox mbs End Sub Sub ShowFolderList(folderspec) 'For this to work go to Menu:- Project-References Dim fs, f, f1, fc, s 'and make sure "Microsoft Scripting Runtime" is ticked. Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(folderspec) Set fc = f.SubFolders fpath1 = fpath 'this looks unnecessary... For Each f1 In fc mbs = mbs & f1.Name 'for the message box at the end mbs = mbs & vbCrLf 'build the string with carriage returns, linefeeds. fpath = fpath1 fpath = fpath & "\" & f1.Name 'modify new path 'Debug.Print fpath 'press ctrl G to open the immediate window and see output. Call ShowFolderList(fpath) 'the recursion bit Next fpath = fpath1 'set a breakpoint here to see how the paths change End Sub 'each time the routine drops out
© 2019 A1VBCode. All rights reserved.