Windows 递归批处理循环查找所有具有给定名称的文件

Windows Batch Loop Recursively to find all files with given name

我正在尽力编写 windows 批处理。 我想列出目录及其所有子目录中具有给定名称“ivy.xml”的所有文件。 示例:

所以输出应该是:

代码:

for /R "Releases" %%f in (ivy.xml) do echo "%%f"

但我得到的是:

当没有通配符(?*)匹配时,for /R 循环只是遍历整个目录树,因此将其扩展 if exist仅限 return 个现有项目:

for /R "Releases" %%f in (ivy.xml) do if exist "%%f" echo "%%f"

如果可能还有名为 ivy.xml 的子目录,您可以通过以下方式排除它们:

for /R "Releases" %%f in (ivy.xml) do if exist "%%f" if not exist "%%f\*" echo "%%f"

鉴于没有匹配模式 ivy.xml? 的文件,您也可以这样做:

for /R "Releases" %%f in (ivy.xml?) do echo "%%f"

另一个选项是 dir 命令,假设在根目录 Releases 中没有名为 ivy.xml 的目录,其内容将变为 returned :

dir /S /B /A:-D "Releases\ivy.xml"

另一个选择是使用 where 命令(PATHEXT 变量在当前会话中被清除,以便不 return 像 ivy.xml.exe 这样的文件,因为实例):

set "PATHEXT="
where /R "Releases" "ivy.xml"