use variables inside if and for loop
Solution 1:
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 entireFOR
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