Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filescripting looping through folders and files 2

Status
Not open for further replies.

Chance1234

IS-IT--Management
Jul 25, 2001
7,871
US
Sub sub_testdata()

Code:
Dim FsO As FileSystemObject
Dim fld As Folder
Dim fld2 As Folders
 Dim fiL As File

Set FsO = New FileSystemObject
Set fld = FsO.GetFolder("H:\Inv\pls\")

For Each fiL In fld.Files
    Debug.Print fiL.Name
Next


End Sub

That works fine, but in the folder pls there are several more folders which contain several more folders etc etc.

what do i need to do so i can go through every one of these folders /

Chance,

Filmmaker, gentleman and soon to be a mac user
,
 
Make it recursive.

'Declare global variable
Public FsO As FileSystemObject

'Trigger sub
Sub ShowAllFiles
Set FsO = New FileSystemObject
ShowFiles(FsO.GetFolder("H:\Inv\pls\"))
set FsO = Nothing
end sub

'Master function
Public Sub ShowFiles (fldr as Folder)

Dim fld As Folder
Dim fiL As File

For Each fiL In fldr.Files
Debug.Print fiL.Name
Next

For Each fld in fldr.Folders
ShowFiles(fld)
Next fld

End Sub

Pete
 
And what about this ?
Public FsO As FileSystemObject

'Trigger sub
Sub ShowAllFiles()
Set FsO = New FileSystemObject
ShowFiles FsO.GetFolder("H:\Inv\pls\")
Set FsO = Nothing
End Sub

'Master function
Public Sub ShowFiles(fldr As Folder)
Dim fld As Folder
Dim fiL As File
For Each fiL In fldr.Files
Debug.Print fiL.Name
Next
For Each fld In fldr.SubFolders
ShowFiles fld
Next fld
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top