Experimenter
Experimenter

Reputation: 167

Find file using for loop in certain folders

I'm trying to find out ugraf.exe files from mapped drive. Below command loops through all folders on current drive to find file - ugraf.exe .

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

I want to find the ugraf.exe in folders starting from ugnx* and existing only in ugii folder.

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

Could anyone please help me?

Upvotes: 1

Views: 77

Answers (2)

Compo
Compo

Reputation: 38622

Unless there are a lot of large directories in the root of O: whose names do not begin with the string ugnx, it seems as if it would be simpler to just search for the file, then check its output for ugnx and ugii directories in the returned path string:

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

You could even do that with the Dir command, instead of using where.exe:

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

If you wanted to capure it from a :

@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

Or using the Dir command instead of 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

If the directories in the root of O: on your ugnx* directory level could be large or many, then just pass that from an initial For /D loop:

@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

Or using the Dir command instead of 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

Or a For /F loop:

@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

And once again, using Dir, instead of 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

Upvotes: 2

Dominique
Dominique

Reputation: 17491

You can pipe your results with findstr commands:

... | findstr /I "O:\ugn" | findstr /I "ugii\ugraf.exe"

The /I stands for case insensitivity.
The findstr /I "O:\ugn" checks that the main directory is indeed "ugnxxxxx".
The findstr /I "ugii\ugraf.exe" makes sure you only get the ones in the "ugii" directory.

Upvotes: 1

Related Questions