使用批处理文件递归删除目录中所有特定类型(扩展名)的文件 [英] Delete all files of specific type (extension) recursively down a directory using a batch file

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

问题描述

我需要删除dir1和dir2中的所有.jpg和.txt文件(例如).

I need to delete all .jpg and .txt files (for example) in dir1 and dir2.

我尝试过的是:

@echo off
FOR %%p IN (C:\testFolder D:\testFolder) DO FOR %%t IN (*.jpg *.txt) DO del /s %%p\%%t

在某些目录中它起作用了;在其他国家则没有.

In some directories it worked; in others it didn't.

例如,这没有做任何事情:

For example, this didn't do anything:

@echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t

第二个片段中我缺少什么?为什么不起作用?

What I'm I missing in the second snippet? Why didn't it work?

推荐答案

您可以将通配符与del命令一起使用,并使用/S进行递归操作.

You can use wildcards with the del command, and /S to do it recursively.

del /S *.jpg

@BmyGuest问为什么一个被低估的答案(del /s c:\*.blaawbg)与我的答案有什么不同.

@BmyGuest asked why a downvoted answer (del /s c:\*.blaawbg) was any different than my answer.

运行del /S *.jpgdel /S C:\*.jpg之间存在巨大差异.第一个命令是从当前位置执行 ,而第二个命令是在整个驱动器上执行 .

There's a huge difference between running del /S *.jpg and del /S C:\*.jpg. The first command is executed from the current location, whereas the second is executed on the whole drive.

在使用第二条命令删除jpg文件的情况下,某些应用程序可能会停止运行,并且最终将丢失所有家庭照片.这很烦人,但是您的计算机仍然可以运行.

In the scenario where you delete jpg files using the second command, some applications might stop working, and you'll end up losing all your family pictures. This is utterly annoying, but your computer will still be able to run.

但是,如果您正在处理某个项目,并且想要删除myProject\dll中的所有dll文件,然后运行以下批处理文件:

However, if you are working on some project, and want to delete all your dll files in myProject\dll, and run the following batch file:

@echo off

REM This short script will only remove dlls from my project... or will it?

cd \myProject\dll
del /S /Q C:\*.dll

然后,您最终将所有dll文件从C:\驱动器中删除.您所有的应用程序都将停止运行,您的计算机将变得无用,并且在下次重新启动时,您将被传送到第四维度,在那里您将永远陷入困境.

Then you end up removing all dll files form your C:\ drive. All of your applications stop working, your computer becomes useless, and at the next reboot you are teleported in the fourth dimension where you will be stuck for eternity.

这里的教训是,如果可以避免的话,不要在驱动器的根部(或其他可能存在危险的位置,例如%windir%)直接运行该命令.始终尽可能在本地运行它们.

The lesson here is not to run such command directly at the root of a drive (or in any other location that might be dangerous, such as %windir%) if you can avoid it. Always run them as locally as possible.

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

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