ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop thru folders (https://www.excelbanter.com/excel-programming/407497-loop-thru-folders.html)

Sam

Loop thru folders
 
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

Chip Pearson

Loop thru folders
 
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)








"Sam" wrote in message
...
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




All times are GMT +1. The time now is 07:32 PM.

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