ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sub Directories (https://www.excelbanter.com/excel-programming/394671-sub-directories.html)

Mike H.

Sub Directories
 
I need to look at all files within a directory and I need to "climb" down
sub-directories within those directories. For example:
FileDir="C:\mystuff"
I am using this loop:
FName = Dir(FileDir)
Do Until FName = ""
do stuff
Loop
But if there is a folder "c:\mystuff\subdir\" I need to look there too.
How do I determine sub-directories (folders) within folders?



Ron de Bruin

Sub Directories
 
See this page Mike
http://www.rondebruin.nl/fso.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Mike H." wrote in message ...
I need to look at all files within a directory and I need to "climb" down
sub-directories within those directories. For example:
FileDir="C:\mystuff"
I am using this loop:
FName = Dir(FileDir)
Do Until FName = ""
do stuff
Loop
But if there is a folder "c:\mystuff\subdir\" I need to look there too.
How do I determine sub-directories (folders) within folders?



Tom Ogilvy

Sub Directories
 

Jim Rech posted recently posted some code that Bill Manville wrote.


Here's a link:


http://groups.google.com/groups?thre...%40tkmsftngp11



http://support.microsoft.com/kb/185476/EN-US/
How To Search Directories to Find or List Files

http://support.microsoft.com/kb/185601/EN-US/
HOW TO: Recursively Search Directories by Using FileSystemObject

http://support.microsoft.com/kb/186118/EN-US/
How To Use FileSystemObject with Visual Basic

--
Regards,
Tom Ogilvy


"Mike H." wrote:

I need to look at all files within a directory and I need to "climb" down
sub-directories within those directories. For example:
FileDir="C:\mystuff"
I am using this loop:
FName = Dir(FileDir)
Do Until FName = ""
do stuff
Loop
But if there is a folder "c:\mystuff\subdir\" I need to look there too.
How do I determine sub-directories (folders) within folders?



JLatham

Sub Directories
 
Doing the digging is a bit difficult using DIR() [which is my preferred
method of digging around in folders anyway, but I recognize the
shortcomings]. Using the File System Object (FSO) works better for this.

Chip Pearson has some good examples of using the FSO with recursion (calling
itself) to work the way down through multiple levels of folders. See
http://www.cpearson.com/excel/RecursionAndFSO.htm
and even
http://www.cpearson.com/excel/CloneFolder.htm

Those should give you some good ideas and a jump start on it all.

"Mike H." wrote:

I need to look at all files within a directory and I need to "climb" down
sub-directories within those directories. For example:
FileDir="C:\mystuff"
I am using this loop:
FName = Dir(FileDir)
Do Until FName = ""
do stuff
Loop
But if there is a folder "c:\mystuff\subdir\" I need to look there too.
How do I determine sub-directories (folders) within folders?



Vergel Adriano

Sub Directories
 
Mike,

Add a reference to the Windows Script Host Object Model and then try
something like this:

Sub ProcessFiles()
Dim oFSO As New FileSystemObject
Dim oFolder As Folder
Dim oSubFolder As Folder
Dim oFile As File

Set oFolder = oFSO.GetFolder("C:\mystuff")

'process files in the starting folder
For Each oFile In oFolder.Files
'do stuff
Debug.Print oFile.Path
Next oFile

'process files in sub folders
For Each oSubFolder In oFolder.SubFolders
For Each oFile In oSubFolder.Files
'do stuff
Debug.Print oFile.Path
Next oFile
Next oSubFolder
End Sub

--
Hope that helps.

Vergel Adriano


"Mike H." wrote:

I need to look at all files within a directory and I need to "climb" down
sub-directories within those directories. For example:
FileDir="C:\mystuff"
I am using this loop:
FName = Dir(FileDir)
Do Until FName = ""
do stuff
Loop
But if there is a folder "c:\mystuff\subdir\" I need to look there too.
How do I determine sub-directories (folders) within folders?



Vergel Adriano

Sub Directories
 
so you don't repeat your code for the file processing part, you can make the
sub a recursive one...

Sub ProcessFiles(strFolder As String)
Dim oFSO As New FileSystemObject
Dim oFolder As Folder
Dim oSubFolder As Folder
Dim oFile As File


Set oFolder = oFSO.GetFolder(strFolder)

'process files in the starting folder
For Each oFile In oFolder.Files
'do stuff
Debug.Print oFile.Path
Next oFile

'process sub folders
For Each oSubFolder In oFolder.SubFolders
ProcessFiles oSubFolder.Path
Next oSubFolder
End Sub

you then would use it like this:

Sub test()
ProcessFiles "c:\mystuff"
End Sub



--
Hope that helps.

Vergel Adriano


"Vergel Adriano" wrote:

Mike,

Add a reference to the Windows Script Host Object Model and then try
something like this:

Sub ProcessFiles()
Dim oFSO As New FileSystemObject
Dim oFolder As Folder
Dim oSubFolder As Folder
Dim oFile As File

Set oFolder = oFSO.GetFolder("C:\mystuff")

'process files in the starting folder
For Each oFile In oFolder.Files
'do stuff
Debug.Print oFile.Path
Next oFile

'process files in sub folders
For Each oSubFolder In oFolder.SubFolders
For Each oFile In oSubFolder.Files
'do stuff
Debug.Print oFile.Path
Next oFile
Next oSubFolder
End Sub

--
Hope that helps.

Vergel Adriano


"Mike H." wrote:

I need to look at all files within a directory and I need to "climb" down
sub-directories within those directories. For example:
FileDir="C:\mystuff"
I am using this loop:
FName = Dir(FileDir)
Do Until FName = ""
do stuff
Loop
But if there is a folder "c:\mystuff\subdir\" I need to look there too.
How do I determine sub-directories (folders) within folders?




All times are GMT +1. The time now is 02:06 PM.

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