vba to delete all files in a folder containing certain text

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the code below that deletes all files in a folder with a specific file name. How can I adapt it so it deletes all files that contain a specific text string?
So in the example, my files all end in 'Summary File v1.pptx' (eg. 'London Summary File v1.pptx', Manchester Summary File v1.pptx' etc..) so I want to find them all and delete them - any ideas?
Thanks.

Code:
Private fso As Object
Public Sub DeleteFiles()


Set fso = CreateObject("Scripting.FileSystemObject")
RecurseDelete "C:\Users\Steve\Desktop\2018 Data\", "Summary File v1.pptx"


End Sub
Private Sub RecurseDelete(currentFolder As String, fileToDelete As String)


Dim thisFolder As Object
Dim subFolder As Object


If fso.FileExists(currentFolder & fileToDelete) Then fso.DeleteFile currentFolder & fileToDelete, True


Set thisFolder = fso.GetFolder(currentFolder)
For Each subFolder In thisFolder.subFolders
    RecurseDelete subFolder.Path & "\", fileToDelete
Next subFolder


End Sub
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
try using a wildcard

Code:
RecurseDelete "C:\Users\Steve\Desktop\2018 Data\", "[COLOR=#ff0000]*[/COLOR]Summary File v1.pptx"
 
Upvote 0
one line of code

Code:
VBA.Kill [COLOR=#333333]"C:\Users\Steve\Desktop\2018 Data\*Summary File v1.pptx"[/COLOR]
 
Last edited:
Upvote 0
Hi Both,
Thanks for your responses.
Tried the RecurseDelete line, but it didn't work
Tried the Kill approach and it works but I need it also to kill files in any of the subfolders of '2018 Data' - can this be achieved using Kill?
Regards,
 
Upvote 0
Hi Both,
Thanks for your responses.
Tried the RecurseDelete line, but it didn't work
Tried the Kill approach and it works but I need it also to kill files in any of the subfolders of '2018 Data' - can this be achieved using Kill?
Regards,


you can get a list of all your files ending with Summary File v1.ppt in all subfolders by doing:

Code:
Dim myFiles As Variant
myFiles = Split(CreateObject("WScript.Shell").EXEC("WHERE /R ""C:\Users\Steve\Desktop\2018 Data"" ""*Summary File v1.pptx""").StdOut.ReadAll, vbNewLine)

myFiles will be an array containing all the files paths
 
Upvote 0
ok, but what would the full code be to kill them all?
thanks
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top