Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sam Sam is offline
external usenet poster
 
Posts: 699
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop to Filter, Name Sheets. If Blank, Exit Loop ryguy7272 Excel Programming 3 February 5th 08 03:41 PM
List folders but not sub folders kaiser Excel Programming 2 July 19th 07 11:14 AM
loop folders macro - Sorry for duplicate posts simplymidori Excel Discussion (Misc queries) 1 July 18th 07 08:34 AM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
Advancing outer Loop Based on criteria of inner loop ExcelMonkey Excel Programming 1 August 15th 05 05:23 PM


All times are GMT +1. The time now is 03:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"