I'm 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?
If ROBOCOPY
is available to you:
@ECHO OFF
MKDIR temporary_pit
ROBOCOPY . temporary_pit /XF *.sdb *.sbk *.log *.bat /MOV >NUL
RMDIR /S /Q temporary_pit
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With