How can I iterate through all folders and its subfolders within a given directory.
Ex.
Say that I get c:\demo
I want to find all subfolders within that folder and all subsequent ones.
c:\demo\sub1\sub2\sub3\sub4 etc...
Thanks,
Printable View
How can I iterate through all folders and its subfolders within a given directory.
Ex.
Say that I get c:\demo
I want to find all subfolders within that folder and all subsequent ones.
c:\demo\sub1\sub2\sub3\sub4 etc...
Thanks,
I'm too lazy to tell how to do it, so I give the code right away :rolleyes: And I'm too lazy to edit the file getting part out of it... but I'm sure you can modify it to store all the directories.
VB Code:
Dim DirList As New Collection, FileList As New Collection, Temp As String On Error Resume Next DirList.Add "C:\" Do While DirList.Count Temp = Dir$(DirList(1), vbDirectory) Do Until Temp = "" If Temp = "." Or Temp = ".." Then 'do nothing ElseIf (GetAttr(FixPathFile(DirList(1), Temp)) And vbDirectory) = vbDirectory Then DirList.Add FixPathFile(DirList(1), Temp) & "\" Else FileList.Add FixPathFile(DirList(1), Temp) End If Temp = Dir Loop DirList.Remove 1 Loop 'now FileList contains all files
VB Code:
'in a module Public Function FixPathFile(ByVal Path As String, File As String) As String If Right$(Path, 1) <> "\" Then Path = Path & "\" FixPathFile = Path & File End Function
This does not return anything.
Spend time reading the code, don't just go ahead and paste. If you read the comment, you'd understand FileList contains a list of all files found in the directories. Now, it shouldn't be a big job to rip this functionality out and add functionality to add found directories to a listbox, for example.
That's a cool way to do it!! I used to use a recursive function before - not anymore!!!
MikkyThomeon
Try: http://www.vbforums.com/showthread.p...hreadid=269578