Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
You have declare the variable mon=Apr and not concatenate with 1. –  avirk May 21 '12 at 2:47

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
    
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 4: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 4:16
    
Bob, the code is here 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 4: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 6:09
    
I have written this code differently 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.