Echoing a URL in Batch

You have not shown us full batch code and therefore I have to guess for reason of variable HTMLPGnr being not defined on reference.

DelayedExpansion is on during the defining of the variable and off when using it.

That sentence let me think of something like following was used in your batch file:

setlocal EnableDelayedExpansion
if HTMLPGnr==1 set /a HTMLNxtpg=2 
if HTMLPGnr==2 set /a HTMLNxtpg=3
if HTMLPGnr==3 set /a HTMLNxtpg=4
if HTMLPGnr==4 set /a HTMLNxtpg=5
if HTMLPGnr==5 set /a HTMLNxtpg=6
if HTMLPGnr==6 set /a HTMLNxtpg=1
endlocal
echo ^<body onload="timer=setTimeout(function(){ window.location='file:///C:/Users/MyUser/Dropbox/Netpage/sources/Page/Page_%HTMLNxtpg%.html';}, %HTMLlen%)" style="background:#408CBD"^>>>sources/Page/Page_%HTMLPGnr%.html

The command setlocal with parameter EnableDelayedExpansion does not only enable delayed environment variable expansion, it makes also a copy of the current environment table.

Every set command modifies the environment variables in the new table. The previous environment variable table is kept in memory in the meantime unmodified. So changing values of already existing environment variables or adding environment variables is done only on the new table.

The command endlocal restores previous mode of delayed expansion which means usually turning it off. And additionally also the current environment variable table is discarded and previous table is restored from memory.

So all set operations resulting in adding, deleting or modifying variables between setlcoal and endlocal are lost after command endlocal.

As variable HTMLNxtpg is created completely useless in a new table with enabled delayed expansion and also useless option /a, this variable is not existing anymore after command endlocal.

As Stephan suggested, those 9 lines of code can be replaced by following 2 lines:

set /a HTMLNxtpg=HTMLPGnr %% 6 + 1
echo ^<body onload="timer=setTimeout(function(){ window.location='file:///C:/Users/MyUser/Dropbox/Netpage/sources/Page/Page_%HTMLNxtpg%.html';}, %HTMLlen%)" style="background:#408CBD"^>>>sources/Page/Page_%HTMLPGnr%.html

But let us look on setlocal and endlocal behavior on a simple example:

@echo off
set "TEST=Hi!"
echo  1. !TEST!
echo  2. %TEST%
setlocal EnableDelayedExpansion
echo  3. !TEST!
echo  4. %TEST%
set "TEST=Hello^!"
echo  5. !TEST!
echo  6. %TEST%
setlocal DisableDelayedExpansion
echo  7. !TEST!
echo  8. %TEST%
set "TEST=Bonjour!"
echo  9. !TEST!
echo 10. %TEST%
endlocal
echo 11. !TEST!
echo 12. %TEST%
endlocal
echo 13. !TEST!
echo 14. %TEST%
set "TEST="
pause

Running this batch file results in the output:

 1. !TEST!
 2. Hi!
 3. Hi!
 4. Hi
 5. Hello!
 6. Hello
 7. !TEST!
 8. Hello!
 9. !TEST!
10. Bonjour!
11. Hello!
12. Hello
13. !TEST!
14. Hi!
  1. !TEST! is output as delayed environment variable expansion is not enabled by default.
  2. Hi! is correct output on referencing value of variable TEST normally while delayed expansion not being enabled.
  3. Hi! is output now with referencing value of variable TEST with delayed expansion because delayed expansion is now enabled and the variable still exists because a copy of entire table was made before.
  4. Hi without explanation mark is output on referencing value of variable TEST normally because of exclamation mark is interpreted by command line interpreter as beginning of a delayed variable reference.
  5. Hello! is correct output with delayed expansion after modifying the variable TEST.
    The modification needs to be done with escaping the exclamation mark as otherwise command line interpreter would interpret ! again as beginning of a delayed expanded variable reference on assigning the new string to variable TEST.
  6. Hello without exclamation mark is output on referencing new value of TEST normally for same reason as before.
  7. !TEST! is output as delayed environment variable expansion is disabled again.
  8. Hello! is the correct output for TEST after disabling delayed expansion and creating one more copy of entire environment variables table.
  9. !TEST! is output as delayed environment variable expansion is still disabled although value of TEST was changed once more.
  10. Bonjour! is the correct output for value of third instance of TEST.
  11. Hello! is output after endlocal which discarded third table and restored also delayed expansion mode.
  12. Hello indicates also that delayed expansion is again active for second table because exclamation mark is again not output.
  13. !TEST! is output because after one more endlocal the second table is also discarded and we are back on initial table with delayed expansion being disabled again.
  14. Hi! is the final output on referencing value of first instance of TEST normally before deleting this variable.

I hope, this simple example helps to understand what the commands setlocal and endlocal do.

Leave a Comment