Make a FOR command file command a command without wildcards?

I saw examples of using batch files that look at directories and process various files that match wildcard expressions, for example:

FOR %%f IN (*.ext) DO (
    ECHO Found ext file %cd%\%%f
)

The above will only match files ending in .extin the current directory. However, what if I want only one exact file name if it exists in this directory? I need something like:

FOR %%f IN (someFile.ext) DO (
    ECHO Found our file name in %cd%
)

However, this does not work; the command FORinterprets "somefile.ext" as a string and always starts the loop FORonce. How can I try to match this exact file name in the current directory without wildcards?

+4
2

Windows " " FOR , . , , , IF :

FOR %%f IN (somefile.ext*) DO (
    IF /I "%%f"=="somefile.ext" (
        ECHO Found somefile.ext in
        ECHO dir: %cd%
        ECHO.
    )
)

... IF, :

IF EXIST somefile.ext (
    ECHO Found somefile.ext in
    ECHO dir: %cd%
    ECHO.
)
0

, , . , .

for , , , /, .

(, " " ), for , , , , .

for %%a in ("somefile.ext?") do if /i "%%~nxa"=="somefile.ext" (
     echo File exists
)

, , , .

for,

if exist "somefile.ext" (
    rem your code
)

,

for %%a in ("somefile.ext") do if exist "%%~fa" (
    rem your code
)

note: , , if exist "file" for %%a in ("file"), )

, .

, , , , ( , somefile.ext\ ), -

(>nul 2>&1 dir /a-d "somefile.ext") && (
    echo file exists
) || (
    echo file does not exist
)

, for

for %%a in ("somefile.ext") do (>nul 2>&1 dir /a-d "%%~fa") && (
    echo %%~fa exists
) || (
    echo %%~fa does not exist
)

dir , (/a-d) , .

note: , , , .

0

Source: https://habr.com/ru/post/1608546/


All Articles