html - Echoing a URL in Batch -
edit: variable wasn't defined properly. have no idea why have found workaround this:
there 6 pages needed. have created 7th page instantly return page 1. , %htmlnxtpg% variable not needed anymore.
i trying create batch file spit out html file user won't need understanding of html make site.
the site needs "live" have iframes redirecting 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 temporary run locally now)
and there variable mentioned in url named %htmlnxtpg% echo command ignoring it. not outputting there leading browser having 404. defining htmlnxtpg variable using
delayedexpansion on during defining of variable , off when using it.
edit: code horribly made , has been fixed comment , answer (@stephan , @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
file:///c:/users/myuser/dropbox/netpage/sources/page/page_.html
while should
file:///c:/users/myuser/dropbox/netpage/sources/page/page_1.html
and 1 outcome of variable
the echo line should output 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.
you have not shown full batch code , therefore have guess reason of variable htmlpgnr
being not defined on reference.
delayedexpansion on during defining of variable , off when using it.
that sentence let me think of following used in 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 parameter enabledelayedexpansion not enable delayed environment variable expansion, makes copy of current environment table.
every set command modifies environment variables in new table. previous environment variable table kept in memory in meantime unmodified. changing values of existing environment variables or adding environment variables done on new table.
the command endlocal restores previous mode of delayed expansion means turning off. , additionally current environment variable table discarded , previous table restored memory.
so set operations resulting in adding, deleting or modifying variables between setlcoal , endlocal lost after command endlocal.
as variable htmlnxtpg
created useless in new table enabled delayed expansion , useless option /a
, variable not existing anymore after command endlocal.
as stephan suggested, 9 lines of code can replaced 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 on setlocal , endlocal behavior on 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 batch file results in 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!
!test!
output delayed environment variable expansion not enabled default.hi!
correct output on referencing value of variabletest
while delayed expansion not being enabled.hi!
output referencing value of variabletest
delayed expansion because delayed expansion enabled , variable still exists because copy of entire table made before.hi
without explanation mark output on referencing value of variabletest
because of exclamation mark interpreted command line interpreter beginning of delayed variable reference.hello!
correct output delayed expansion after modifying variabletest
.
modification needs done escaping exclamation mark otherwise command line interpreter interpret!
again beginning of delayed expanded variable reference on assigning new string variabletest
.hello
without exclamation mark output on referencing new value oftest
same reason before.!test!
output delayed environment variable expansion disabled again.hello!
correct outputtest
after disabling delayed expansion , creating 1 more copy of entire environment variables table.!test!
output delayed environment variable expansion still disabled although value oftest
changed once more.bonjour!
correct output value of third instance oftest
.hello!
output after endlocal discarded third table , restored delayed expansion mode.hello
indicates delayed expansion again active second table because exclamation mark again not output.!test!
output because after 1 more endlocal second table discarded , on initial table delayed expansion being disabled again.hi!
final output on referencing value of first instance oftest
before deleting variable.
i hope, simple example helps understand commands setlocal , endlocal do.
Comments
Post a Comment