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!
)