im trying to make a batch file on windows for deleting all the files in the current directory but excluding 4 file extensions (log, sdb, sdk, bat).
I have tried the Forfiles command on windows but this delete everything on my current folder (even the bat file). My command is:
@ECHO OFF
FORFILES /M *.* /C "cmd /c IF NOT @ext=="sdb" (IF NOT @ext=="sbk" (IF NOT @ext=="log" (IF NOT @ext=="bat" DEL @FILE)))" /Q
How can I make it work?
Thanks a lot!
\
IF /I
(case insensitive) optionThis should work
But the above is very slow, and it sure is a lot to type.
The following is much simpler and faster.
Also, you can do something like this:
-- Mario
If
ROBOCOPY
is available to you:That is, you are creating a temporary subdirectory, moving the files that are to be deleted to it (which is fast because, as the destination directory is on the same drive, only file names are relocated, not the files' contents), then deleting the subdirectory.
This is about as simple as I could get this script. They wanted to keep the macro files (.dqy) but recursively delete everything else older than 14 days.
Runs in the current directory (be careful when testing).
I ran across this topic searching for a way to delete hundres of files created by a virus. Non of the solutions really worked for me, so I figured out how to do it from a command line. I needed only to keep 2 extensions (mail archive). This did the trick:
I use the /R to work recursive: look in all subfolders. The %~xf looks at the extension only (for some reason it didn't work without it). I use the quotes "%f" at the delete command to cover the windows long names with spaces (especially in folder names). Also for some reason, adding spaces before and behing the == gave errors.