Loop thru folders

S

Sam

I put together this code that deletes error log files (those with no
extension FileType: "File").

Sub NoExtFiles()
fPath = Range("Path").Value
Set fso = CreateObject _
("Scripting.FileSystemObject")
Set folder = fso.GetFolder(fPath)

For Each file In folder.Files
If fso.GetExtensionName(file) = "" Then
Debug.Print file.Name
file.Delete
End If
Next
Set folder = Nothing
Set fso = Nothing
End Sub

It works well for a fixed path; however I want to loop through each folder
in the path (defined in the range "Path") and delete all files explained
above.

Can anyone help?

Thanks in advance,

Sam
 
C

Chip Pearson

Try something like the following.

Dim TopF As Object
Dim SubF As Object
Dim F As Object
Dim FSO As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set TopF = FSO.GetFolder(Range("Path").Value)
For Each SubF In TopF.SubFolders
For Each F In SubF.Files
' test each F file
Next F
Next SubF
For Each F In TopF.Files
' test each F File
Next F


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top