“FOR” command for a specific file?

2019-05-11 14:58发布

问题:

Seems like I can use the FOR command recursively to search for wildcards such as

FOR /R %F IN (*.ASM) DO @ECHO %F

This successfully prints out all files with the .ASM extension that exist somewhere within the current directory tree

However, if I'm searching for a specific filename, this doesn't work at all:

FOR /R %F IN (LECTURE3_CODE.ASM) DO @ECHO %F

The output of the latter command seems to print out <path>\LECTURE3_CODE.ASM once for every single file that exists in the working directory tree.

Is there some way to get this command to work the way I want it to?

回答1:

Very simple:

FOR /R %f IN (*.ASM) DO (if "%~nf"=="LECTURE3_CODE" ECHO %f)

Done!



回答2:

This will work for your original line - the wildcard is the key here.

FOR /R %F IN (LECTURE3_CODE.AS?) DO @ECHO %F