简体   繁体   English

删除包含在不同子文件夹中的同名文件

[英]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.

相关问题 批处理文件以在具有特定名称的文件夹和子文件夹上运行命令 - batch file to run command on folder and sub-folders with specific name 批处理文件将文件夹移动到子文件夹 - Batch file to move folders to sub-folders 批处理文件以运行具有不同名称的文件夹和子文件夹中的 all.bat 文件 - Batch file to run all .bat files in folders and sub-folders with different names 创建一个BAT文件以共享多个文件夹及其子文件夹 - Creating a BAT file to share multiple folders, and their sub-folders Windows批处理文件-显示所有子文件夹 - Windows batch file - display all sub-folders 如何仅扫描精确文件名(通过批处理文件)? //如何从不同的子文件夹复制(并保留)同名文件? - How to scan only for EXACT file names (via batch file)? // How to copy (and keep) eponymous files from different sub-folders? 批处理文件删除列表中不包含的文件夹 - Batch File To Delete Folders Not Contained In a List Excel VBA 根据单元格值搜索文件夹和子文件夹获取文件路径和数据 - Excel VBA search based on cell values into folders and sub-folders to get the file path and data 子文件夹允许的最大深度是多少? - What is the maximum allowed depth of sub-folders? 计算VBScript中的子文件夹数 - Count Number Of Sub-Folders in VBScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM