0

I was going over an example batch file that was able to display a variable outside of a loop:

@echo off
setlocal EnableDelayedExpansion 
:: count to 5 storing the results in a variable
set _tst=0
FOR /l %%G in (1,1,5) Do (echo [!_tst!] & set /a _tst+=1)
echo Total = %_tst%

It's able to echo %_tst% because it's declared at the top before the loop.

I tried it with a batch file that I'm currently using:

@echo off
cls
setlocal EnableDelayedExpansion

set drive=R:
set counter=0

FOR /F "tokens=*" %%c IN ('dir %USERPROFILE%\Backup /B') DO (set /A counter+=1)

if %counter% GTR 0 (
    echo Total # of folders: %counter%  
) else (
    echo No folders to move
)

It works, but, when I try to check if a drive is available before executing the loop, I have the use !counter! to access the variable, like so:

If I don't, it just says "Please press any key to continue." because of the pause.

@echo off
cls

setlocal EnableDelayedExpansion

set drive=R:
set counter=0

if exist %drive% (

    FOR /F "tokens=*" %%c IN ('dir %USERPROFILE%\Backup /B') DO (set /A counter+=1)

    if !counter! GTR 0 (
        echo You have !counter! folder(s)
    ) else (
        echo No folders to move
    )
)
pause
exit /b

Why is it that when I have the if statement checking if my drive is available I have to use !counter!?

  • 1
    If you change a variable inside a block of code (`if exist` in your sample) you need delayed expansion to retrieve the changed value while inside the same block of code – MC ND Oct 07 '16 at 17:53
  • @MCND - The if exist is sort of blocking the delayed expansion from being seen? Where should I put the setlocal EnableDelayedExpansion? – AnnabelleRosemond Oct 07 '16 at 18:10
  • 1
    `setlocal enabledelayedexpansion` does not mean that any variable read operation is delayed until code execution. It means that you can use the `!var!` syntax to indicate that this read operation must be delayed. `%var%` read operations will be replaced at parse time with or without `enabledelayedexpansion`. – MC ND Oct 07 '16 at 18:36
  • 1
    Maybe [this](http://stackoverflow.com/a/30177832/2861476) could help. – MC ND Oct 07 '16 at 18:41
  • @MCND - Many thanks for the link. – AnnabelleRosemond Oct 07 '16 at 18:46

1 Answers1

0

You have a problem with the ) in the text file(s) - this closes the inner if !counter! GTR 0 block. Then it sees another ) which closes the outer if exist %drive% block.

To fix this, escape the ) in the echo:

if exist %drive% (

    FOR /F "tokens=*" %%c IN ('dir %USERPROFILE%\Backup /B') DO (set /A counter+=1)

    if !counter! GTR 0 (
        echo You have !counter! folder^(s^)
    ) else (
        echo No folders to move
    )
)
John D
  • 1,627
  • 1
  • 11
  • 10
  • Many thanks. Corrected. I'm still a little confused, how would I access the counter variable outside the for loop? should I use !counter! or %counter% – AnnabelleRosemond Oct 07 '16 at 18:42
  • A block `( ... )` counts as a single line to the parser so all %variable% are expanded with their values before the block runs. The !variable! delays the expansion until run time. So inside the block, use !variable! – John D Oct 07 '16 at 18:49
  • Just to be clear if I wanted to access my counter outside my for loop, I would use !counter!, correct? – AnnabelleRosemond Oct 07 '16 at 18:52
  • That's right - you'd use !counter!. I tried the above with %counter% and it returns 0. – John D Oct 07 '16 at 18:54
  • Been trying for hours trying to understand why it returned 0. Thanks! – AnnabelleRosemond Oct 07 '16 at 18:56
  • I don't have a Backup folder so I used Documents instead which I know has files in it and %counter% returned 0 inside the if exist block. – John D Oct 07 '16 at 18:56