[英]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"
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.