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?
我怎样才能做到这一点?
7 个解决方案
#1
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%
#2
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文件。
#3
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复制子目录
#4
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 :)
搏一搏 :)
#5
Try researching here.
尝试在这里研究。
Although it doesn't give you exactly what you are asking, I would say it's a good starting point.
虽然它并不能完全满足您的要求,但我认为这是一个很好的起点。
#6
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是您不想删除的类型。
#7
FORFILES /s /p c:\dir /c "if not @ext==.txt del /f @file"
#1
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%
#2
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文件。
#3
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复制子目录
#4
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 :)
搏一搏 :)
#5
Try researching here.
尝试在这里研究。
Although it doesn't give you exactly what you are asking, I would say it's a good starting point.
虽然它并不能完全满足您的要求,但我认为这是一个很好的起点。
#6
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是您不想删除的类型。
#7
FORFILES /s /p c:\dir /c "if not @ext==.txt del /f @file"