batch-file - 在某些文件夹中使用 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
有人可以帮我吗?
解决方案
除非根目录中有很多O:
名称不以 string 开头的大目录ugnx
,否则似乎只搜索文件会更简单,然后检查其输出ugnx
和ugii
返回的路径字符串中的目录:
@"%__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-loop中捕获它:
@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
推荐阅读
- python - "Pyaudio" pip install fails for every package even after upgrading pip
- amazon-web-services - Does Lambda event source mappings support streams in other regions?
- python - Dynamically generated schedule interval?
- typescript - 无法在 Typescript 中推断父接口的通用参数
- kotlin - How can I throw an SQLServerException (or SQLException) with Mockito?
- kubernetes - Kubernetes Cassandra java.lang.IllegalStateException: Unable to compute ceiling for max when histogram overflowed
- angular - No validation by default until fill fields in angular
- html - Angular Material - Setting custom style for matInput when disabled
- apache - kubernetes 上的 Apache 下载持久性卷文件而不是渲染
- aws-lambda - 我的 lambda 函数的一部分执行了两次