echo !state! displays something when echo is off so no good.
rem !state! just shows the 7 letter string !state! so no good. Too bad; this would have been perfect.
set state=!state! surprises me that it also doesn't show the expansion.
Apparently, what is displayed when you run a FOR loop with echo on, is first an echo of the FOR and its DO block, and then more echos of lines, this time showing each line in the block that is to be run (even though they look almost identical to the first lines displayed); but there's only the FOR loop variable expansion at that time, not environment variables. Running the code below, the LOOP variable %i performs perfectly (shows its expanded value even in a REM). But !state! content is hard to display.
BTW I know that I could use IF or GOTO logic to conditionally ECHO !state! using a boolean, but would prefer not to add the "noise".
Experience tells me that someone will say that I can already see !state! when I set it to begin with below, but that's just an artificiality of the silly example I built to help you see what I'm shooting for - which is how to see !somevar! in a FOR loop, while ECHO is on, without using ECHO !somevar!.
Code: Select all
rem echo off
set state=NO
setlocal enabledelayedexpansion
for /F %%i in ('dir^| findstr /C:"Directory"') do (
rem i is %%i
set state=yes
echo state is !state! & rem eventually expands but shows during echo off
set state=!state! & rem doesn't visibly expand
rem state is !state! & rem doesn't visibly expand
rem state is %state% & rem expands the "wrong" thing, undelayed
)
rem The following SET lines are to be removed; are just FYI for this post
set state
endlocal
set state
Code: Select all
C:\>rem echo off
C:\>set state=NO
C:\>setlocal enabledelayedexpansion
C:\>for /F %i in ('dir| findstr /C:"Directory"') do (
rem i is %i
set state=yes
echo state is !state! & rem eventually expands but shows when echo off
set state=!state! & rem doesn't expand
rem state is NO & rem expands the wrong thing
rem state is !state! & rem doesn't expand
)
C:\>(
rem i is Directory
set state=yes
echo state is !state! & rem eventually expands but shows when echo off
set state=!state! & rem doesn't expand
rem state is NO & rem expands the wrong thing
rem state is !state! & rem doesn't expand
)
state is yes
C:\>set state
state=yes
C:\>endlocal
C:\>set state
state=NO