Reputation: 12151
If I run this batch file like this it fails within the IF loop. If I remove the IF loop (and the ending ")" it works as expected. (t is never set to what you typed).
IF 1==1 (
set /p t=type in value
echo You typed: %t%
set t=%t% plus Suffix
echo Your value + suffix: %t%
)
pause
Upvotes: 0
Views: 98
Reputation: 1705
you need setlocal enabledelayedexpansion
@echo off
setlocal enabledelayedexpansion
IF 1==1 (
set /p "t=type in value: "
echo You typed: !t!
set "t=!t! plus Suffix"
echo Your value + suffix: !t!
)
pause
endlocal
exit/B
When the command processor finds a block (anything between parentheses), parses it completely and expand variables to the value they have when the block is evaluated. If you update a variable value within a block, you need to enable delayed expansion for the variable to reflect changes made. Also you must change %var%
to !var!
Consider the following
@echo off
setlocal enabledelayedexpansion
set "var=Round 0"
echo( ----------------------------------------------
for /L %%i in (1,1,5) do (
set "var=Round %%i"
echo( var is %var% [not using delayed expansion]
echo( var is !var! [using delayed expansion]
echo( ----------------------------------------------
)
echo( After block %var% !var! are the same
pause
exit/B
Upvotes: 1