G
Greg Maxey
The following is a condensed version of a larger piece of code that I have
been working on to list files in folders. Currenty it will drill down
looking for files two sub-folder layers deep. What I have spent half the
day on is some method of looping through a single procedure or funtion to
drill down to the last sub-folder layer in a given folder.
I figured out how to do that if each folder only has one subfolder, but that
isn't practical. Thanks for any assistance, ideas, or even a statement that
it just can't be done.
Option Explicit
'Set reference to Microsoft Scripting Runtime
Public fso As New FileSystemObject
Dim oFld As Folder
Dim oFile As File
Dim oRng As Word.Range
Sub ListFolder()
Dim sPath As String
sPath = "C:\Batch"
Set oFld = fso.GetFolder(sPath)
For Each oFile In oFld.Files
Debug.Print oFile.Name
Next oFile
If oFld.SubFolders.Count > 0 Then
ListSubFolders oFld.SubFolders
End If
End Sub
Function ListSubFolders(ByRef oSubFolders As Folders)
Dim oSubFolder As Scripting.Folder
For Each oSubFolder In oSubFolders
For Each oFile In oSubFolder.Files
Debug.Print oSubFolder.Name & " " & oFile.Name
Next oFile
If oSubFolder.SubFolders.Count > 0 Then
ListSubFolders2 oSubFolder.SubFolders
End If
Next oSubFolder
Set oSubFolder = Nothing
End Function
Function ListSubFolders2(ByRef oSubFolders As Folders)
Dim oSubFolder2 As Scripting.Folder
For Each oSubFolder2 In oSubFolders
For Each oFile In oSubFolder2.Files
Debug.Print oSubFolder2.Name & " "; oFile.Name
Next oFile
Next oSubFolder2
Set oSubFolder2 = Nothing
End Function
--
Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org
McCain/Palin '08 !!!
been working on to list files in folders. Currenty it will drill down
looking for files two sub-folder layers deep. What I have spent half the
day on is some method of looping through a single procedure or funtion to
drill down to the last sub-folder layer in a given folder.
I figured out how to do that if each folder only has one subfolder, but that
isn't practical. Thanks for any assistance, ideas, or even a statement that
it just can't be done.
Option Explicit
'Set reference to Microsoft Scripting Runtime
Public fso As New FileSystemObject
Dim oFld As Folder
Dim oFile As File
Dim oRng As Word.Range
Sub ListFolder()
Dim sPath As String
sPath = "C:\Batch"
Set oFld = fso.GetFolder(sPath)
For Each oFile In oFld.Files
Debug.Print oFile.Name
Next oFile
If oFld.SubFolders.Count > 0 Then
ListSubFolders oFld.SubFolders
End If
End Sub
Function ListSubFolders(ByRef oSubFolders As Folders)
Dim oSubFolder As Scripting.Folder
For Each oSubFolder In oSubFolders
For Each oFile In oSubFolder.Files
Debug.Print oSubFolder.Name & " " & oFile.Name
Next oFile
If oSubFolder.SubFolders.Count > 0 Then
ListSubFolders2 oSubFolder.SubFolders
End If
Next oSubFolder
Set oSubFolder = Nothing
End Function
Function ListSubFolders2(ByRef oSubFolders As Folders)
Dim oSubFolder2 As Scripting.Folder
For Each oSubFolder2 In oSubFolders
For Each oFile In oSubFolder2.Files
Debug.Print oSubFolder2.Name & " "; oFile.Name
Next oFile
Next oSubFolder2
Set oSubFolder2 = Nothing
End Function
--
Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org
McCain/Palin '08 !!!