VBA Code to delete files in a folder that do not contain a specific text string

Raler

New Member
Joined
Dec 26, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am interested in writing a macro using VBA to loop through the file names within a folder and delete all files that are not named a specific file name. In the example below, I would like the macro to delete all files that are not named "Prepaid Maintenance.xlsx". So far, the only way I've been able to manage this is by creating a line of code to delete the files that are not named "Prepaid Maintenance.xlsx". Is there a better way to go about this?

1703605469963.png


1703606064536.png
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Google 'vba loop over files' to see how to loop over them. Use an IF block to test for the file name, such as
VBA Code:
If f.Name <> "myFileNameHere" Then
   line to kill the file here
End If
This sort of thing can be risky. If anyone, including you who might forget about this, places other files such as Word documents in that folder, they are gone too. If that can happen, you might also want to test for the file extension.
 
Upvote 0
Thanks! I was able to create the below macro to solve my problem.

1703626526750.png
 
Upvote 0
That works ok? I ask because you set filename to a path without specifying a file name, so at first, filename will never be "". That means for the first iteration, filename will never be "Prepaid Maintenance.xlsx". I think the first test might be the prior level of the directory so it may not be "" either (it sometimes can return "." or ".." if you are high enough in the directory tree). It may not matter if the first check returns either of those or the folder above, but I'm not sure. This would be along the lines of my approach:
VBA Code:
fName = Dir("C:\...\" & "Prepaid Maintenance.xlsx")
Do While Len(fName)=0
   Kill fName
   fname = Dir
Loop

You also ought to maintain proper indentation to make your code easier to follow. Short ones like that, not much problem. Long procedure, it's a drudge to follow.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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