批量回显 URL [英] Echoing a URL in Batch

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

问题描述

变量定义不正确.我不知道为什么,但我找到了解决方法:

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

只需要 6 页.我创建了第 7 页,该页将立即返回到第 1 页.因此不再需要 %HTMLNxtpg% 变量.

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.

我正在尝试创建一个批处理文件,该文件将输出一个 HTML 文件,因此用户无需对 HTML 有任何了解即可制作他们的网站.

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">

(这个 URL 是临时的,所以它现在会在本地运行)

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

并且在名为的 URL 中提到了一个变量%HTMLNxtpg%但是 Echo 命令完全忽略了它.它没有输出任何导致浏览器具有 404 的内容.我正在使用

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 在定义变量时开启,在使用时关闭.

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

此代码制作得很糟糕,并已通过评论和答案(@Stephan 和 @Mofi)修复

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

我得到的结果是

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

虽然应该是

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

1 是变量的结果

应该输出 body 标签的 Echo 行:

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

提前致谢.

推荐答案

您没有向我们展示完整的批处理代码,因此我不得不猜测变量 HTMLPGnr 未在引用中定义的原因.

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 在定义变量时开启,在使用时关闭.

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

带有参数 EnableDelayedExpansion 的命令 setlocal 不仅启用延迟环境变量扩展,它还复制当前环境表.

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

每个set 命令都会修改新表中的环境变量.之前的环境变量表保留在内存中,同时未修改.因此,更改现有环境变量的值或添加环境变量仅在新表上完成.

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.

命令 endlocal 恢复先前的延迟扩展模式,这意味着通常将其关闭.此外,当前环境变量表也被丢弃,从内存中恢复以前的表.

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.

所以在endlocal命令之后,所有导致在setlcoalendlocal之间添加、删除或修改变量的set操作都将丢失强>.

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

由于变量 HTMLNxtpg 在启用延迟扩展的新表中创建完全无用,并且选项 /a 也无用,因此在命令 后该变量不再存在本地端.

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.

正如 Stephan 建议的那样,这 9 行代码可以替换为以下 2 行:

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

但是让我们通过一个简单的例子来看看 setlocalendlocal 的行为:

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! 输出,因为默认不启用延迟环境变量扩展.
  2. Hi! 在未启用延迟扩展的情况下,在引用变量TEST 时正常输出正确.
  3. Hi! 现在输出带有延迟扩展的变量 TEST 的引用值,因为现在启用了延迟扩展并且变量仍然存在,因为制作了整个表的副本之前.
  4. Hi 通常在引用变量 TEST 的引用值时输出没有解释标记,因为命令行解释器将感叹号解释为延迟变量引用的开始.莉>
  5. Hello! 是修改变量TEST后延迟扩展的正确输出.
    修改需要通过转义感叹号来完成,否则命令行解释器会在将新字符串分配给变量 TEST 时再次将 ! 解释为延迟扩展变量引用的开始.
  6. Hello 在引用 TEST 的新值时通常会输出不带感叹号的内容,原因与之前相同.
  7. !TEST! 输出为延迟环境变量扩展再次禁用.
  8. Hello! 是在禁用延迟扩展并创建整个环境变量表的另一个副本后 TEST 的正确输出.
  9. !TEST! 输出为延迟环境变量扩展仍然禁用,尽管 TEST 的值再次更改.
  10. Bonjour!TEST 的第三个实例的值的正确输出.
  11. Hello! 是在 endlocal 之后输出的,它丢弃了第三个表并恢复了延迟扩展模式.
  12. Hello 还表示第二个表的延迟扩展再次处于活动状态,因为再次不输出感叹号.
  13. !TEST! 是输出,因为在多一个 endlocal 之后,第二个表也被丢弃,我们回到初始表,延迟扩展再次被禁用.
  14. Hi! 是在删除此变量之前正常引用 TEST 的第一个实例的值的最终输出.
  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.

我希望,这个简单的例子有助于理解 setlocalendlocal 命令的作用.

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

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

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