Computer Hope
Microsoft => Microsoft DOS => Topic started by: vij_guy on February 18, 2008, 03:12:47 PM
-
Once I get into a 'if' block or 'for' loop in a batch script, it doesn't recognize the SET variable. I found something about CMD /V:ON to enable the delayed execution in a batch script; but then how would you run it in a batch script. When I enabled the CMD /V, the batch script won't run any further beyond that.
I finally had to change the logic using GOTO and do away with getting into an 'if' block. Any solution to this?
-
Welcome to the CH forums.
Please read >>this (http://www.computerhope.com/forum/index.php/topic,33328.0.html)<< and post your script.
-
It is possible to enable delayed expansion within a script, using the setlocal command with the enabledelayedexpansion modifier.
This first script won't display values for %number% and %answer%, because those variables are expanded at runtime, and will be empty.
@echo off
for %%N in (1 2 3 4) do (
set number=%%N
echo number=%number%
)
set count=11
if "%count%"=="11" (
set answer=yes
echo answer=%answer%
)
This second script will work because delayed expansion is enabled and because !number! and !answer! have exclamation marks instead of percent signs.
Once you are outside the IF block or loop you can use the variables with % signs and they will show up.
@echo off
setlocal enabledelayedexpansion
for %%N in (1 2 3 4) do (
set number=%%N
echo number=!number!
)
set count=11
if "%count%"=="11" (
set answer=yes
echo answer=!answer!
)