How to delete *.* excluding some extensions?

前端 未结 6 2153
夕颜
夕颜 2020-12-10 05:46

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 th

6条回答
  •  萌比男神i
    2020-12-10 06:42

    • internal quotes must be escaped with \
    • you probably want IF /I (case insensitive) option
    • you should use @ISDIR to exclude directories
    • DEL /Q option was after last quote, should be before last quote, but it isn't needed
    • parentheses are not needed
    • FORFILES /M option isn't needed since your mask is "all files"

    This should work

    @echo off
    forfiles /c "cmd /c if @isdir equ FALSE if /i not @ext==\"sdb\" if /i not @ext==\"sbk\" if /i not @ext==\"log\" if /i not @ext==\"bat\" del @file"
    

    But the above is very slow, and it sure is a lot to type.

    The following is much simpler and faster.

    @echo off
    for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".sdb .sbk .log .bat"') do del "%%F"
    

提交回复
热议问题