ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to traverse subdirectories (https://www.excelbanter.com/excel-programming/412837-how-traverse-subdirectories.html)

Ben

How to traverse subdirectories
 
Hi all,

I need to go to a particular directory then from there I would like to
traverse all the subdirectories beneath it. can you show me how?

I would also like to use a debug.print statement print out the paths.
Thanks for share your thoughts.

Ben


Chip Pearson

How to traverse subdirectories
 
You can use the following code. Sub AAAA is the starting point where you
specify the starting folder. The ListSubFolders procedure recursively lists
all the subfolders (and subfolders of subfolders etc).

Sub AAAA()
Dim FSO As Scripting.FileSystemObject
Dim StartFolderName As String
Dim StartFolder As Scripting.Folder

StartFolderName = "C:\FrontPage Webs" '<<<<<<<<< CHANGE
Set FSO = New Scripting.FileSystemObject
Set StartFolder = FSO.GetFolder(StartFolderName)
Debug.Print "Start: " & StartFolder.Path
ListSubFolders StartFolder
End Sub

Sub ListSubFolders(OfFolder As Scripting.Folder)
Dim SubFolder As Scripting.Folder
For Each SubFolder In OfFolder.SubFolders
Debug.Print SubFolder.Path
If SubFolder.SubFolders.Count 0 Then
ListSubFolders SubFolder
End If
Next SubFolder
End Sub

See also http://www.cpearson.com/Excel/FolderTree.aspx for example code for
many ways to list subfolders and/or files.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"Ben" wrote in message
...
Hi all,

I need to go to a particular directory then from there I would like to
traverse all the subdirectories beneath it. can you show me how?

I would also like to use a debug.print statement print out the paths.
Thanks for share your thoughts.

Ben



Ben

How to traverse subdirectories
 
Chip,

Thanks so much. That was exactly what I was looking for.

Ben

--



"Chip Pearson" wrote:

You can use the following code. Sub AAAA is the starting point where you
specify the starting folder. The ListSubFolders procedure recursively lists
all the subfolders (and subfolders of subfolders etc).

Sub AAAA()
Dim FSO As Scripting.FileSystemObject
Dim StartFolderName As String
Dim StartFolder As Scripting.Folder

StartFolderName = "C:\FrontPage Webs" '<<<<<<<<< CHANGE
Set FSO = New Scripting.FileSystemObject
Set StartFolder = FSO.GetFolder(StartFolderName)
Debug.Print "Start: " & StartFolder.Path
ListSubFolders StartFolder
End Sub

Sub ListSubFolders(OfFolder As Scripting.Folder)
Dim SubFolder As Scripting.Folder
For Each SubFolder In OfFolder.SubFolders
Debug.Print SubFolder.Path
If SubFolder.SubFolders.Count 0 Then
ListSubFolders SubFolder
End If
Next SubFolder
End Sub

See also http://www.cpearson.com/Excel/FolderTree.aspx for example code for
many ways to list subfolders and/or files.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"Ben" wrote in message
...
Hi all,

I need to go to a particular directory then from there I would like to
traverse all the subdirectories beneath it. can you show me how?

I would also like to use a debug.print statement print out the paths.
Thanks for share your thoughts.

Ben




All times are GMT +1. The time now is 11:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com