Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete *.* excluding some extensions?

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?

like image 409
Francisco Chavez Avatar asked Feb 24 '12 02:02

Francisco Chavez


1 Answers

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.

like image 180
Andriy M Avatar answered Jan 02 '23 20:01

Andriy M