在呼应批网址 [英] Echoing a URL in Batch

查看:135
本文介绍了在呼应批网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:变量没有被正确定义。我不知道为什么,但我已经找到了解决方法如下:

有6只需要网页。我创建了一个7页,将立即返回第1页,因此,不再需要在%HTMLNxtpg%变量。


我想创建一个批处理文件,它会吐出一个HTML文件,因此用户将不再需要任何HTML了解,使他们的网站。

该网站需要活的,所以我有内部框架重定向到对方使用

 <身体的onload =计时器= setTimeout的(函数(){了window.location ='文件:/// C:/用户/ MYUSER / Dropbox的/ PageMDB /来源/页/Page_%HTMLNxtpg%.html';}%HTMLlen%)的风格=背景:#408CBD>

(此URL是暂时的,因此将目前在本地运行)

和存在名为在URL中提及的变量
%HTMLNxtpg%
但是echo命令是完全忽略它。它是不是有什么输出领先进入浏览器有一个404,我使用的定义变量HTMLNxtpg

DelayedExpansion使用时变量的定义和关闭期间是上

编辑:这code是可怕制成,已被固定由一个评论和答案(@Stephan和@Mofi)

 如果HTMLPGnr == 1套/年HTMLNxtpg = 2
如果HTMLPGnr == 2套/年HTMLNxtpg = 3
如果HTMLPGnr == 3套/年HTMLNxtpg = 4
如果HTMLPGnr == 4套/年HTMLNxtpg = 5
如果HTMLPGnr == 5套/年HTMLNxtpg = 6
如果HTMLPGnr == 6套/年HTMLNxtpg = 1

结果我得到的是

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

虽然应

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

和1是可变的结果

回声行应该输出身体标记:

 回声^<身体的onload =计时器= setTimeout的(函数(){了window.location ='文件:/// C:/用户/ MYUSER / Dropbox的/ Netpage /来源/Page/Page_%HTMLNxtpg%.html';}%HTMLlen%)的风格=背景:#408CBD^>>>源/页/ Page_%HTMLPGnr%的.html

先谢谢了。


解决方案

您还没有告诉我们整批code,因此我不得不猜测原因的变量 HTMLPGnr 的引用而没有定义。


  

DelayedExpansion使用时变量的定义和关闭期间是上


这句话让我觉得像下面是在您的批处理文件中使用:

  SETLOCAL EnableDelayedExpansion
如果HTMLPGnr == 1套/年HTMLNxtpg = 2
如果HTMLPGnr == 2套/年HTMLNxtpg = 3
如果HTMLPGnr == 3套/年HTMLNxtpg = 4
如果HTMLPGnr == 4套/年HTMLNxtpg = 5
如果HTMLPGnr == 5套/年HTMLNxtpg = 6
如果HTMLPGnr == 6套/年HTMLNxtpg = 1
ENDLOCAL
回声^<身体的onload =计时器= setTimeout的(函数(){了window.location ='文件:/// C:/Users/MyUser/Dropbox/Netpage/sources/Page/Page_%HTMLNxtpg%.html';} ,%HTMLlen%)的风格=背景:#408CBD^>>>源/页/ Page_%HTMLPGnr%的.html

该命令的 SETLOCAL 带参数的 EnableDelayedExpansion 的并不只启用延迟的环境变量扩展,这也使得当前环境表的副本。

每一个设置命令修改在新表中的环境变量。在previous环境变量表保存在内存中同时修改。因此,改变现有的环境变量或添加环境变量的值是对新表只是做了。

该命令的 ENDLOCAL 延迟恢复扩张这意味着通常把它关掉的previous模式。 ,另外也是当前环境变量表被丢弃,previous表是从内存中恢复。

因此​​,所有的设置致使之间添加,删除或修改变量的操作的 setlcoal ENDLOCAL 命令后,将丢失的 ENDLOCAL

作为变量 HTMLNxtpg 创建完全没用在一个新表启用延迟扩展和也没用选项 / A ,该变量没有命令后,现有的再 ENDLOCAL

的建议,这9条线code可以通过以下2行取代>

 设置/一HTMLNxtpg = HTMLPGnr %% 6 + 1
回声^<身体的onload =计时器= setTimeout的(函数(){了window.location ='文件:/// C:/Users/MyUser/Dropbox/Netpage/sources/Page/Page_%HTMLNxtpg%.html';} ,%HTMLlen%)的风格=背景:#408CBD^>>>源/页/ Page_%HTMLPGnr%的.html

不过,让我们看看在 SETLOCAL ENDLOCAL 在一个简单的例子行为:

 关闭@echo
设置TEST =嗨!
1.呼应!TEST!
2.回声测试%%
SETLOCAL EnableDelayedExpansion
3.呼应!TEST!
4.回声测试%%
设置TEST = ^你好!
5.呼应!测试!
6.回声测试%%
SETLOCAL DisableDelayedExpansion
7.呼应!TEST!
8.回声测试%%
设置TEST =卓悦!
9.呼应!TEST!
10.回声测试%%
ENDLOCAL
11.呼应!TEST!
12.回声测试%%
ENDLOCAL
13.呼应!TEST!
14.回声测试%%
集TEST =
暂停

在输出运行这个批处理文件的结果:

  1!测试!
 2.嗨!
 嗨!
 4.喜
 5.您好!
 6.你好
 7.!TEST!
 8.你好!
 9.!TEST!
10.卓悦!
11.您好!
12.你好
13.!TEST!
14.嗨!


  1. !TEST!作为延迟的环境变量扩展输出不是默认启用的。

  2. 你好!是引用变量测试值正确的输出正常而没有被启用延迟扩展。

  3. 你好!输出现在引用变量测试值延迟扩展,因为延迟扩展现已启用,并该变量仍然存在,因为整个表的副本之前完成。

  4. 你好不用解释标志是引用变量测试值输出通常是因为感叹号是间$ P通过命令行间preTER $ PTED作为延迟变量引用的开始。

  5. 您好!是延迟扩展正确的输出修改变量后测试。结果
    修改需要与逃避感叹号另有命令行间preTER做会间preT 再次作为延迟扩展变量引用的开始分配新的字符串变量测试

  6. 您好没有感叹号是引用测试通常像以前同样的原因,新的价值输出。

  7. !TEST!作为延迟的环境变量扩展输出再次关闭。

  8. 您好!测试正确的输出禁用延迟扩展和创造多一个整个环境变量的副本后,表。

  9. !TEST!作为延迟的环境变量扩展输出尽管测试的价值仍然被禁用改为一次

  10. 的Bonjour!测试第三实例的值正确的输出。

  11. 您好!是输出后的 ENDLOCAL 它丢弃第三个表,恢复也推迟扩张模式。

  12. 您好表示也是推迟扩张是再为第二个表激活,因为感叹号再次不输出。

  13. !TEST!是输出,因为后一个更多的 ENDLOCAL 第二个表也被丢弃,我们又回到初始表延迟扩张之中再次关闭。

  14. 你好!是引用的 TEST 正常删除此变量之前。<一审价值的最终输出/ LI>

我希望,这个简单的例子有助于理解了这些命令的 SETLOCAL ENDLOCAL 做的。

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.

解决方案

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.

这篇关于在呼应批网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆