LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Delete certain files in all subfolders of specified folder

Chip (Churck),

When I run the code it errors out on the first line (Dim FSO AS
Scripting.FileSystemObject) with the error "user-defined type not defined".
Any ideas on how to fix this?

Thanks

"Chip Pearson" wrote:


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.


 
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
How to List the names of the subfolders present in the folder (path of folder is given in the textbox by user ) divya Excel Programming 3 November 30th 06 11:34 AM
copy subfolders, replace text in files and save files in copied subfolders pieros Excel Programming 0 November 1st 05 12:08 PM
Delete all files within a folder (incl subfolders) Steph[_3_] Excel Programming 1 September 30th 04 09:59 PM
Trouble making a report of all Files within a Folder and all Subfolders? SuperJas Excel Programming 2 April 2nd 04 02:41 AM


All times are GMT +1. The time now is 11:14 AM.

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

About Us

"It's about Microsoft Excel"