删除批处理文件中具有特定扩展名的文件 [英] Delete file with specific extension in batch file

查看:87
本文介绍了删除批处理文件中具有特定扩展名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在批处理文件中递归删除所有具有特定扩展名的文件.

I would like to recursively delete all files with a specific extension in a batch file.

我知道以下命令:

del /s *.ext

但是,这在Windows上也会删除具有其他扩展名(例如)的文件..ext1或.ext2.造成这种情况的原因似乎是该文件的8.3文件名以.ext结尾,因此扩展名较长的文件也将匹配.

However, this does on Windows also delete files with other extensions like e.g. .ext1 or .ext2 . The reason for this seems to be that the 8.3 file name of such a file ends with .ext and therefore also the files with longer extensions are matched.

我正在寻找上述命令的替代品,该命令以递归方式删除所有扩展名为.ext的文件,但保留扩展名更长的文件.

I am looking for a replacement to the command above that recursively deletes all files with .ext extension but keeps files with longer extensions.

推荐答案

where 命令的工作方式略有不同(关于通配符和短名称).放置一个 for/f 循环,就完成了.您的示例将转换为:

the where command works a bit differently (in regards to wildcards and short-names). Put a for /f loop around, and you're done. Your example would then translate to:

for /f "delims=" %%a in ('where /r . *.ext') do ECHO del "%%a"

注意:我通过回显命令解除了 del 命令的武装.在确定问题确实符合您的要求之后,请在故障排除后删除 ECHO .

Note: I disarmed the del command by just echoing it. Remove the ECHO after troubleshooting, when you are sure it does exactly what you want.

这篇关于删除批处理文件中具有特定扩展名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆