Deleting files with given extension but not inside given folder

How would I go about creating a button which checks the current folder and all subfolders recursively for files with certain extensions and delete them unless they reside in a folder with a certain name, at which point the recursion should stop?

Examples: (C:\ is the current folder.)
"Delete all .exe and .bat files unless they are in a folder called Important."

C:\alice.exe would be deleted.
C:\temp\bob.bat would be deleted.
C:\work\charlie.exe would be deleted.
C:\work\Important\daniel.bat would NOT be deleted.
C:\work\Important\final\elena.exe would NOT be deleted.

A filter like this will match those files:

You could use it with Tools > Find Files (Advanced) to find the files you want to delete, which gives you a chance to check what will be deleted first.

Or you could set up a button/hotkey to use the filter with the Delete command, but be careful to test it first, particularly with different files/folders selected, as the way delete filters work on the selected files & folders (vs things below selected folders) isn't always what you might expect.

1 Like