1

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

我怎样才能做到这一点?

4

7 回答 7

2

在一位同事请求帮助删除文件夹和所有子文件夹中某些类型的文件除外的所有文件,同时保持目录结构后,我编写了这个批处理文件。我建议在尝试此操作之前备份您的文件夹。只需打开记事本并粘贴以下内容,将其保存为 .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%
于 2011-03-18T15:44:42.293 回答
1

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

搏一搏 :)

于 2009-08-25T10:40:20.233 回答
1

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

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文件。

于 2009-05-14T14:23:12.647 回答
1

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

/s 复制子目录

于 2009-05-14T14:33:35.337 回答
0

你可以试试这个:

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

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

于 2012-02-01T03:54:27.237 回答
0

尝试在这里研究。

尽管它不能准确地告诉您您的要求,但我会说这是一个很好的起点。

于 2009-05-14T14:23:52.270 回答
0
FORFILES /s /p c:\dir /c "if not @ext==.txt del /f @file"
于 2013-04-03T18:32:14.727 回答