首页 > 解决方案 > 在某些文件夹中使用 for 循环查找文件

问题描述

我正在尝试从映射驱动器中找出 ugraf.exe 文件。下面的命令循环遍历当前驱动器上的所有文件夹以查找文件 - ugraf.exe.

For /R %A In (ugraf.exe)Do @Echo %~dpnxA

我想找到从ugraf.exe文件夹开始ugnx*并仅存在于文件夹中的ugii文件夹。

O:\ugnxxxxx\...\wntx64\kits\ugii\ugraf.exe

有人可以帮我吗?

标签: batch-file

解决方案


除非根目录中有很多O:名称不以 string 开头的大目录ugnx,否则似乎只搜索文件会更简单,然后检查其输出ugnxugii返回的路径字符串中的目录:

@"%__AppDir__%where.exe" /R "O:\." "ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$"

您甚至可以使用Dir命令来做到这一点,而不是使用where.exe

@Dir /B /S /A:-D "O:\ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$"

如果您想从中捕获它:

@For /F "Delims=" %%G In ('""%__AppDir__%where.exe" /R "O:\." "ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$""') Do @Echo %%G

或者使用Dir命令代替where.exe

@For /F "Delims=" %%G In ('"Dir /B /S /A:-D "%%G\ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$""') Do @Echo %%G

O:如果您的目录级别的根ugnx*目录可能很大或很多,那么只需从初始For /D循环中传递它:

@For /D %%G In ("O:\ungx*") Do @For /F "Delims=" %%H In ('""%__AppDir__%where.exe" /R "%%G" "ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$""') Do @Echo %%H

或者使用Dir命令代替where.exe

@For /D %%G In ("O:\ungx*") Do @For /F "Delims=" %%H In ('"Dir /B /S /A:-D "%%G\ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$""') Do @Echo %%G

For /F循环:

@For /F "Delims=" %%G In ('Dir /B /S /A:D "O:\ungx*" 2^>NUL') Do @For /F "Delims=" %%H In ('""%__AppDir__%where.exe" /R "%%G" "ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$""') Do @Echo %%H

再一次,使用Dir, 而不是where.exe

@For /F "Delims=" %%G In ('Dir /B /S /A:D "O:\ungx*" 2^>NUL') Do @For /F "Delims=" %%H In ('"Dir /B /S /A:-D "%%G\ugraf.exe" 2>NUL | "%__AppDir__%findstr.exe" /I /R "^O:\\ugnx*\\ugii\\ugraf\.exe$""') Do @Echo %%H

推荐阅读