I am having difficulty understanding how batch file localization works. Here is an example to demonstrate my confusion
@echo off
set x=5
set y=1
setlocal enabledelayedexpansion
if %y% GEQ 1 (
set x=6
echo %x%
echo !x!
)
echo %x%
endlocal
echo %x%
This prints, 5 6 6 5
And if I remove the IF statement, it prints, 6 6 6 5
What a mess!
Why is it like this? In Linux bash files I know exactly what will happen
but not here.
Please explain to me what is going on in each of the print statements, and what the best way is to code for variables that are modified in IF blocks.
I had to use setlocal to solve another problem
@echo off
set x=5
set y=1
if %y% GEQ 1 (
set x=6
echo %x%
)
echo %x%
This print 5 6
despite my expectation of 6 6
. Then I learned about setlocal to fix this that is now another headache.