[英]Delete file with same name contained in different sub-folders
For some reason, I have a copy of dismhost.exe
in a lot of folders, inside temp
folder; 由于某些原因,我在
temp
文件夹内的许多文件夹中dismhost.exe
的副本; what I want to do is delete every instance of it, which are inside folders inside temp
. 我想要做的是删除它的每个实例,这些实例都在
temp
文件夹内。
So the structure is as follows: 所以结构如下:
/temp
/folder1
dismhost.exe
/folder2
dismhost.exe
/folder3
dismhost.exe
...
I first tried 我第一次尝试
rm ./*/dismhost.exe
but then I remembered there is no rm
in windows, so I tried with rd
with same arguments. 但是后来我想起在Windows中没有
rm
,所以我尝试使用具有相同参数的rd
。 That raised an error, saying that the *
modifier is not valid. 这引发了一个错误,说
*
修饰符无效。
How can I achieve this? 我该如何实现?
This can be done using a FOR loop iterating over a list of files returned by a recursive DIR search. 这可以通过使用FOR循环遍历递归DIR搜索返回的文件列表来完成。 When you are satisfied with the output, remove
ECHO
in order to actually delete the files. 当您对输出感到满意时,请删除
ECHO
以便实际上删除文件。
FOR /F "usebackq tokens=*" %f IN (`DIR /S /B /A:-D \temp\dismhost.exe`) DO (ECHO DEL "%~f")
If this is placed into a .bat script, be sure to double the % characters on the variable. 如果将其放置在.bat脚本中,请确保将变量上的%字符加倍。
FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D \temp\dismhost.exe`) DO (
ECHO DEL "%%~f"
)
或使用递归删除:
del /s \temp\dismhost.exe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.