4

I have the following code:

echo off
set n=11
set m=12
set /a nme=3
set /a mdiff=nme-1
pause
if %n% NEQ %m% (
    if %mdiff% LEQ 3 (
        for /l %%C in (1,1,3) do (
            if %%C EQU 1 (
                set mon=Apr
            )
set num=1%mon%
        )
    )
)
echo %num%
pause

which gives me output 1 instead of 1Apr. However when I place set num=1%mon% outside all if and for loops it gives correct result.

Please explain me what happened here and how to obtain the correct result inside the loops.

Also, what is the maximum depth of if and for levels?

Anna
  • 75

1 Answers1

7

What you need to do is put a SetLocal EnableDelayedExpansion at the top of your script and use !s around your variables.

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. When delayed expansion is in effect variables may be referenced using !variable_name! (in addition to the normal %variable_name% )

Delayed variable expansion is often useful when working with FOR Loops, normally an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script.

Basically, the for loop gets parsed once. Each iteration of the loop, the statements get executes. By enabling this option, the variables can change on execution, without reparsing, i.e. within the loop.

@echo off
SetLocal EnableDelayedExpansion

set n=11 set m=12 set /a nme=3 set /a mdiff=nme-1 pause if %n% NEQ %m% ( if %mdiff% LEQ 3 ( for /l %%C in (1,1,3) do ( if %%C EQU 1 ( set mon=Apr set num=1!mon! ) ) ) ) echo %num% pause

Bob
  • 61,637
  • Thank you Bob! it works well, however in a similar code below it does not help. Please help to found out what happened.@echo off SetLocal EnableDelayedExpansion

    set ys=11 set ye=12 set /a nme=9 set jan=Jan

    if %ys% NEQ %ye% ( echo in if pause set /a mdiff=nme-1 if %mdiff% LEQ 9 ( for /l %%B in (1,1,%mdiff%) do ( for /1 %%C in (1,1,31) do ( if %%B EQU 1 ( set monthe=Jan echo !monthe! ) if %%B EQU 2 ( set monthe=Feb echo !monthe! ) REM xcopy "C:\Tests\conversion\Data.%ye%\Month.%monthe%\Day.0%%C" ) ) ) ) echo %monthe% pause

    – Anna May 21 '12 at 04:13
  • @Anna I can't read that. Please put it on Pastebin (and give me the link in a comment) or add it to your question. It would help if you explained what you want it to do. – Bob May 21 '12 at 04:16
  • Bob, the code is here http://pastebin.com/5xisFyGV. It is just a fregment of a big script which convert files from one format into another. The main difficulty is that files should be taked from particular folders by date range (e.g. 01/02/2011 - 02/04/2012). Moreover output files should be placed in a separate directory but coping the original hierarchy. I'm not a programmer so my code is quite long. This bit of code is a core which would search a data by year-month-date. And it doesn't work starting after the first "pause" line I don't know why. Please help – Anna May 21 '12 at 04:33
  • @Anna This kind of help is better done through chat (you'll need to register a SuperUser account, then we can create a chat room) or through a new question, not comments. I will say you probably should be using !nme! in line 13, but there may be other errors. Also, if this answer helped you with this particular question (variable in loop), please accept the answer by clicking the tick to the left. – Bob May 21 '12 at 06:09
  • I have written this code differently http://pastebin.com/WzJffFKh. It is executable but it looks like variable 'mark' does not change its value even if the condition is true. What the reason can be? – Anna May 21 '12 at 22:59
  • @Anna Keep in mind that there's no Boolean in Windows batch files. true is the literal string "true". Use 1 and 0. Once again, if this answer helped, please accept the answer by clicking the tick to the left. It prevents the question from coming up as unanswered and being automatically bumped every now and then. – Bob May 21 '12 at 23:15
  • It works now!!!! For those seeking a script to do something in a range of dates this can be helpful http://pastebin.com/amtrzvNq – Anna May 21 '12 at 23:39