Home |
Search |
Today's Posts |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
How to List the names of the subfolders present in the folder (path of folder is given in the textbox by user ) | Excel Programming | |||
copy subfolders, replace text in files and save files in copied subfolders | Excel Programming | |||
Delete all files within a folder (incl subfolders) | Excel Programming | |||
Trouble making a report of all Files within a Folder and all Subfolders? | Excel Programming |