如何删除*.*排除某些扩展名? [英] How to delete *.* excluding some extensions?

查看:97
本文介绍了如何删除*.*排除某些扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows上创建一个批处理文件,以删除当前目录中的所有文件,但不包括4个文件扩展名(log,sdb,SDK,bat).

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

我在Windows上尝试过Forfiles命令,但这会删除当前文件夹中的所有内容(甚至是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

我如何使它工作?

推荐答案

  • 内部引号必须使用\
  • 进行转义
  • 您可能需要IF /I(不区分大小写)选项
  • 您应该使用@ISDIR排除目录
  • DEL/Q选项在最后一个引号之后,应该在最后一个引号之前,但是不是必需的
  • 不需要括号
  • 不需要使用FORFILES/M选项,因为您的掩码是所有文件"
    • 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"
    • 这应该有效

      @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"
      

      这篇关于如何删除*.*排除某些扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆