2

EDIT: The variable wasn't defined properly. I have no idea why but I have found a workaround for this:

There are only 6 pages needed. I have created a 7th page that will instantly return to page 1. and thus the %HTMLNxtpg% variable is not needed anymore.


I am trying to create a Batch file that will spit out an HTML file so the user won't need any understanding of HTML to make their site.

The site needs to be "live" so I have iframes redirecting into each other using

<body onload="timer=setTimeout(function(){ window.location='file:///C:/Users/MyUser/Dropbox/PageMDB/sources/Page/Page_%HTMLNxtpg%.html';}, %HTMLlen%)" style="background:#408CBD">

(This URL is temporary so it will run locally for now)

and there is a Variable mentioned in the URL named %HTMLNxtpg% But the Echo command is completely ignoring it. it is not outputting anything there leading into the browser having a 404. I am defining the HTMLNxtpg variable using

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

EDIT: This code is horribly made and has been fixed by a comment and answer (@Stephan and @Mofi)

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

The result I get is

file:///C:/Users/MyUser/Dropbox/Netpage/sources/Page/Page_.html

While it should be

file:///C:/Users/MyUser/Dropbox/Netpage/sources/Page/Page_1.html

and the 1 is the outcome of a variable

The Echo line that should output the body tag:

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

Thanks in advance.

  • 1
    show more of your code. I assume, you use the shown line within a commandblock (`if` or `for`) – Stephan Jul 11 '15 at 10:18
  • by the way: `set /a x=x%%6+1` would toggle 1-2-3-4-5-6-1-2-... – Stephan Jul 11 '15 at 10:22
  • @Stephan The problem is that the variable in the URL is not showing up, while it is defined. and I've tried your set command but I can't really figure it out (i'm such a n00b) The first line is the complete thing it should ECHO but it doesn't do the HTMLNxtpg variable, but the following variable (HTMLlen) does work. –  Jul 11 '15 at 10:29
  • when it doesn't show up, it isn't defined. No matter if you _think_ it is or not. That's why I asked for more code (might be the [delayed expansion trap](http://stackoverflow.com/a/30284028/2152082)) – Stephan Jul 11 '15 at 10:34
  • @Stephan I am not using blocks for the reason that I have an extremely basic understanding of Batch and would run into problems like that and abandon my code. I am defining the Nxtpg variable at the beginning of the file and using it at the end, so it should stay defined. –  Jul 11 '15 at 10:39
  • in your case, you can replace all six `if HTMLPGnr==...`lines with only one command: `set /a HTMLNxtpg=HTMLPGnr %% 6 + 1` (maybe no big benefit with n=6, but imagine 20 or 100). `%%` is the "modulo" operator (rest of a division, e.g. 7/6=1 rest **1**) – Stephan Jul 11 '15 at 13:32

1 Answers1

1

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.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143