Thread
:
Delete certain files in all subfolders of specified folder
View Single Post
#
5
Posted to microsoft.public.excel.programming
Don Guillett
external usenet poster
Posts: 10,124
Delete certain files in all subfolders of specified folder
Aw come on, Churck has a nice ring to it.
http://www.google.com/search?sourcei...US219&q=churck
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
"Chip Pearson" wrote in message
...
I have read Churck Pearson' recursion
Who?
The following code should work. Change the line marked with <<< to the
desired folder.
Sub DeleteABC()
Dim FSO As Scripting.FileSystemObject
Dim FF As Scripting.Folder
Set FSO = New Scripting.FileSystemObject
Set FF = FSO.GetFolder("C:\Test") '<<< Change Folder
DoOneFolder FF, FSO
End Sub
Sub DoOneFolder(FF As Scripting.Folder, FSO As
Scripting.FileSystemObject)
Dim SubF As Scripting.Folder
Dim F As Scripting.File
For Each F In FF.Files
If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
Recycle F.Path
End If
Next F
For Each SubF In FF.SubFolders
DoOneFolder SubF, FSO
Next SubF
End Sub
The DoOneFolder function uses the Recycle library, at
http://www.cpearson.com/Zips/modRecycleVBA.zip
to send files to the
Windows Recycle Bin rather than using Kill do delete the file, so you
can get back any files that might be deleted. Unload and unzip the
link above and then Import (in VBA, File menu, Import File item) the
bas file into your project.
The code requires a reference (in VBA, Tools men, References item) to
the "Microsoft Scripting Runtime" library.
If you want to confirm each deletion, use the following:
Sub DoOneFolder(FF As Scripting.Folder, _
FSO As Scripting.FileSystemObject)
Dim Res As VbMsgBoxResult
Dim SubF As Scripting.Folder
Dim F As Scripting.File
For Each F In FF.Files
If StrComp(Left(F.Name, 3), "ABC", vbTextCompare) = 0 Then
Res = MsgBox("Delete file: '" & _
F.Path & "'?", vbYesNoCancel)
Select Case Res
Case vbYes
Recycle F.Path
Case vbNo
' do nothing
Case vbCancel
Exit Sub
End Select
End If
Next F
For Each SubF In FF.SubFolders
DoOneFolder SubF, FSO
Next SubF
End Sub
Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
On Sat, 6 Dec 2008 07:52:00 -0800, hurlbut777
wrote:
I have a folder, C:\Test, that contains many subfolders. Within these
subfolders there are many excel files. I would like to build a macro that
loops through all excel files and subfolders within C:\Test, and deletes
any
excel files that start with "ABC". I have read Churck Pearson' recursion
information, but am afraid I will start deleting stuff I shouldn't if I
attempt to rambo code on my own.
Reply With Quote
Don Guillett
View Public Profile
Find all posts by Don Guillett