rasnauf
rasnauf

Reputation: 353

Batch for /f command why does my program not work?

For some reason, none of the statements under the first for statement will run. I tried to enter a pause command directly under the for statement and it did not pause.

@echo off

net use Z: X

Z:

for /f %%i in ('dir /a:d /b') do (

    set doit=1

    for /f %%x in ('dir /b Z:\%%i') do if %%x == X set doit=0

    if %doit% == 1 echo %%i
)

I think the problem is in the first for statement since not even the pause command will run even when I put it directly under the for statement. But I tried typing the for statement in command prompt (without the extra % in %%i) and the statements underneath ran just fine. So is there a different syntax in batch files from cmd (other than %% instead of %)?

Upvotes: 1

Views: 1321

Answers (3)

JosefZ
JosefZ

Reputation: 30113

If I can understand your code, all the manipulation with %doit% or !doit! (npocmaka is right with EnableDelayedExpansion) and inner for /f loop seems to be rather redundant.

@echo off
net use Z: \\BRMH-FS01\StuHome$
Z:

for /f "delims=" %%i in ('dir /a:d /b') do If not exist "Z:\%%~i\Fisher.png" echo %%i

However, if one would do more actions conditionally, in next code snippet I keep set "doit=0" in else branch of if statement as any code block enclosed in () parentheses should not stay empty...

for /f "delims=" %%i in ('dir /a:d /b') do (

    If not exist "Z:\%%~i\Fisher.png" (
        set "doit=1"
        echo %%i
    ) else (
        set "doit=0"
    )
)

Resources (required reading):

Upvotes: 1

Magoo
Magoo

Reputation: 79983

Regrettably, "It still didn't work" doesn't provide any information useful in solving the problem. You should say what happened and what ou expected to happen as well as indication whether the target file existed and where.

I'd suggest a number of minor fixes:

for /f %%i in ('dir /a:d /b') do (
    set doit=1
    for /f %%x in ('dir /b Z:\%%i') do if /I "%%x"=="Fisher.png" set "doit="
    if DEFINED doit echo %%i
)

Your "pause failed" report may indicate that Z: contains no subdirectories as that for /f will list the subdirectories in basic format but if no subdirectories exist then it will not execute the main body. Hence, if you are looking for Z:\Fisher.png it will not be detected, otherwise you appear to be attempting to detect that file in any subdirectory under Z:\

Similarly, with your second for, you are matching filenames found against your target name. Quoting each side of the comparison-operator == ensures that if a filename which contains separators like Space is encountered, proper if syntax is maintained. The /I makes the comparison case-insensitive if required.

It's a standard response to use delayededexpansion and !var! but the method I've advocated means you can avoid delayedexpansion. The if defined syntax operates on the current stats of the target variables - either it is currently defined or not so if you set it to some value, then use set "doit=" to cler the value, if defined doit works on whether or not the flag variable doit has been cleared by the inner for loop.

Why you use this complex mathod rather than

if exist "z:\%%i\Fisher.png" echo %%i

is a mystery. No doubt you have your reasons.

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

You need delayed expansion:

@echo off

net use Z: \\BRMH-FS01\StuHome$

Z:
setlocal enableDelayedExpansion
for /f %%i in ('dir /a:d /b') do (

    set doit=1

    for /f %%x in ('dir /b Z:\%%i') do if %%x == Fisher.png set doit=0

    if !doit! == 1 echo %%i
)

Upvotes: 1

Related Questions