Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Loop to Filter, Name Sheets. If Blank, Exit Loop | Excel Programming | |||
List folders but not sub folders | Excel Programming | |||
loop folders macro - Sorry for duplicate posts | Excel Discussion (Misc queries) | |||
Naming Worksheets - Loop within a loop issue | Excel Programming | |||
Advancing outer Loop Based on criteria of inner loop | Excel Programming |