简体   繁体   English

在批处理文件中,如何删除非特定类型的所有文件

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

I'm writing a batch file that needs to delete all the files in a certain dir and all of it's subdirectories except those of a specific type. 我正在编写一个批处理文件,该文件需要删除特定目录及其所有子目录中的所有文件, 特定类型的子目录除外

How can I do this? 我怎样才能做到这一点?

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! 只需打开记事本并粘贴以下内容,然后将其另存为.bat(而不是.txt)即可。祝您好运! ~Carolyn 〜卡罗琳

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%

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 :) 搏一搏 :)

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 .java files. 保留目录结构,但仅复制.java文件。

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复制子目录

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

Try researching here . 在这里尝试研究。

Although it doesn't give you exactly what you are asking, I would say it's a good starting point. 尽管它不能完全满足您的要求,但我想这是一个很好的起点。

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. 变量地址是目录,变量类型是您不想删除的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Windows批处理文件删除不包含某些字符的文件? - How can I Delete Files That Do Not Contain Certain Characters Using Windows Batch File? 如何删除子目录中的文件和文件夹,但在Ruby中跳过某些文件名的删除? - How do I delete files and folders in sub-directories but skip delete for certain file names in Ruby? Windows批处理文件删除桌面上的所有文件 - Windows batch file to delete all files on desktop Windows批处理-删除特定年龄和类型的文件并将其列出 - Windows batch - Delete files of certain age and type and list them 是否可以使用Windows批处理文件删除某些rar文件? - Is it possible to use a Windows batch file to delete certain rar files? 批处理文件在文件夹中经过一定时间后删除文件? - Batch file to delete files after a certain amount of time in folder? 如何使批处理文件删除它自己的目录? - How do I make a batch file delete it's own directory? 如何递归删除给定路径中的所有文件,但具有特定文件扩展名的文件除外? - How to recursively delete all files from a given path except files with a certain file extension? 如何从Windows批处理文件中删除Y子文件夹中的N类N文件? - How to delete N files of X type from Y subfolders from a Windows batch file? 使用批处理删除文件夹中除列表中的文件以外的所有文件 - Delete all files in folder except file in list using batch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM