windows 在批处理文件中,如何删除不是某种类型的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/863552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:29:05  来源:igfitidea点击:

In a batch file, how do I delete all files NOT of a certain type

windowsbatch-filecmddos

提问by Monster

I'm writing a batch file that needs to delete all the files in a certain dir and all of it's subdirectories exceptthose of a specific type.

我正在编写一个批处理文件,需要删除某个目录中的所有文件及其所有子目录,特定类型的文件除外

How can I do this?

我怎样才能做到这一点?

回答by Carolyn Defoore

I wrote this batch file after a coworker asked for help with deleting all the files EXCEPT for those of certain types from a folder and all subfolders, while maintaining the directory structure. I would recommend backing up your folder before attempting this. Just open up notepad and paste the following, save it as a .bat instead of a .txt Good luck! ~Carolyn

我写了这个批处理文件是在一位同事请求帮助删除文件夹和所有子文件夹中某些类型的文件之外的所有文件,同时保持目录结构。我建议在尝试之前备份您的文件夹。只需打开记事本并粘贴以下内容,将其保存为 .bat 而不是 .txt 祝你好运!~卡罗琳

REM Use at your own risk, it does a mass DELETE of everything!

SET /p ExcludeFiles=What file type should be kept (NOT deleted)? Type the file name(s) inside parantheses. example: (pdf) or (shp dbf shx)     
SET /p MapDrive=What drive letter is the folder in? example: c or n     
SET /p Directory=Drag the folder you would like to modify into this command prompt then press ENTER.     

%MapDrive%:
cd %Directory%

attrib +a *.* /s
echo %date%
for %%i in %ExcludeFiles% do attrib -a *.%%i /s
echo %date%
del %Directory%\*.* /s /a:a /q

echo %date%
attrib +a %Directory%\*.* /s
echo %date%

回答by Joey

You might try something along the lines of

你可以尝试一些类似的东西

for /f "usebackq delims=" %i in (`dir /s /b *`) do if not %~xi==.txt del %i

For your question in the comment you can try the following:

对于评论中的问题,您可以尝试以下操作:

robocopy source_folder target_folder *.java /s

or

或者

xcopy *.java target_folder /s

which keeps the directory structure but only copies .javafiles.

它保留目录结构但只复制.java文件。

回答by Martin Beckett

safest way is to copy all the files you do want and delete the rest.
XCOPY *.java c:\new_directory /s

最安全的方法是复制您需要的所有文件并删除其余文件。
XCOPY *.java c:\new_directory /s

The /s copies subdirectories

/s 复制子目录

回答by Martin Beckett

I'm using the solution found here: How can I delete all files/subdirs except for some files in DOS?

我正在使用此处找到的解决方案: 如何删除除 DOS 中的某些文件之外的所有文件/子目录?

give it a go :)

搏一搏 :)

回答by Eppz

Try researching here.

尝试在这里研究。

Although it doesn't give you exactly what you are asking, I would say it's a good starting point.

虽然它并没有完全满足您的要求,但我会说这是一个很好的起点。

回答by Alex

You can try this:

你可以试试这个:

ECHO DIR %address% /S /B | FIND /C %theType%>temptemp.txt
FOR /f %%a IN (temptemp.txt) DO DEL %%a
DEL temptemp.txt

The variable, address, is the directory and the variable, theType, is the type you do not want to delete.

变量 address 是目录,变量 theType 是您不想删除的类型。

回答by esac

FORFILES /s /p c:\dir /c "if not @ext==.txt del /f @file"