Delete *.* excluding some extensions

2019-01-19 15:00发布

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!

6条回答
该账号已被封号
2楼-- · 2019-01-19 15:30
  • 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"
查看更多
Bombasti
3楼-- · 2019-01-19 15:31

Also, you can do something like this:

@echo off
attrib -r -s *.*
attrib +r +s *.sdb
attrib +r +s *.sbk
attrib +r +s *.log
attrib +r +s *.bat
del *.* /S /Q
attrib -r -s *.sdb
attrib -r -s *.sbk
attrib -r -s *.log
attrib -r -s *.bat

-- Mario

查看更多
smile是对你的礼貌
4楼-- · 2019-01-19 15:38

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.

查看更多
The star\"
5楼-- · 2019-01-19 15:39
forfiles /s /c "cmd /c (if NOT @ext==\"dqy\" del /s /q @path)" /D -14

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).

查看更多
萌系小妹纸
6楼-- · 2019-01-19 15:42
@echo off
setlocal EnableDelayedExpansion
set exclude=.log.sdb.sdk.bat.
for %%f in (*.*) do (
   if /I "%exclude%" == "!exclude:%%~Xf.=!" del "%%f"
)
查看更多
Rolldiameter
7楼-- · 2019-01-19 15:44

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:

for /R %f in (*) do if not %~xf==.ex1 if not %~xf==*.ex2 del "%f"

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.

查看更多
登录 后发表回答