Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 471
Default 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?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default 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?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default 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?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default 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?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default 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?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default 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?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Directories wardides[_6_] Excel Programming 1 October 15th 04 12:24 AM
sub directories again Shailesh Shah[_2_] Excel Programming 5 February 9th 04 09:32 PM
sub directories again Mike[_67_] Excel Programming 2 February 7th 04 12:06 AM
sub directories Mike Excel Programming 5 February 6th 04 10:15 PM
Directories Jeff[_24_] Excel Programming 4 January 20th 04 09:26 AM


All times are GMT +1. The time now is 02:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"