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

标签 windows batch-file cmd dos

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

我该怎么做?

最佳答案

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

关于windows - 在批处理文件中,如何删除所有非特定类型的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/863552/

相关文章:

java - 将唯一的文件扩展名扫描到变量中

windows - windows批处理中自动回答输入提示

python - pip 如何删除带有前导破折号 : "-pkgname" 的错误安装包

c++ - 如何从 C/C++ 不带控制台窗口执行批处理文件?

variables - 如何在批处理文件中添加变量

java - 如何扫描可执行文件的搜索路径

c# - Windows 编程 : Simple SFTP Transfer Application

windows - 通过 PsExec 在远程计算机中执行批处理文件

c# - 在程序内存中执行 BATCH 脚本

windows - 需要运行一个 .exe,然后在 ~10 秒后将其终止并移至批处理文件中的下一个命令