我正在编写一个批处理文件,该文件需要删除某个目录中的所有文件及其所有子目录,但特定类型的文件除外。
我怎样才能做到这一点?
在一位同事请求帮助删除文件夹和所有子文件夹中某些类型的文件除外的所有文件,同时保持目录结构后,我编写了这个批处理文件。我建议在尝试此操作之前备份您的文件夹。只需打开记事本并粘贴以下内容,将其保存为 .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%
我正在使用此处找到的解决方案: 如何删除除 DOS 中的某些文件之外的所有文件/子目录?
搏一搏 :)
你可以尝试一些类似的东西
for /f "usebackq delims=" %i in (`dir /s /b *`) do if not %~xi==.txt del %i
对于评论中的问题,您可以尝试以下操作:
robocopy source_folder target_folder *.java /s
或者
xcopy *.java target_folder /s
它保留目录结构但只复制.java
文件。
最安全的方法是复制您想要的所有文件并删除其余文件。
XCOPY *.java c:\new_directory /s
/s 复制子目录
你可以试试这个:
ECHO DIR %address% /S /B | FIND /C %theType%>temptemp.txt
FOR /f %%a IN (temptemp.txt) DO DEL %%a
DEL temptemp.txt
变量 address 是目录,变量 theType 是您不想删除的类型。
尝试在这里研究。
尽管它不能准确地告诉您您的要求,但我会说这是一个很好的起点。
FORFILES /s /p c:\dir /c "if not @ext==.txt del /f @file"