变量内部的批变量在调用时不工作 [英] Batch variable inside a variable not working when called

查看:100
本文介绍了变量内部的批变量在调用时不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  setlocal EnableDelayedExpansion $ 

b $ b set browser = chrome.exe
set i = 0
for(test.txt)中的%% f do(
set i = 0
for / =%% l in(%% f)do(
set / A i + = 1
set line!i!= %% l
))
::上面读取Test.txt的内容并将每行保存到由两个变量组成的不同变量
set x = 0
:ReadLines
如果%x%gtr%i%(
goto Completed)
set / a x + = 1
set / a odd =%x %%% 2
if%odd%== 1(
set Address =!line %x%!
echo!Line%x%!
::上面的echo返回来自文本文件的URL
echo%地址%
::但是上面这里,尽管%地址%已经设置为!Line%x%!
pause
::当我尝试使用时,echo回来没有任何东西(然后是回声关闭或回声打开)我的代码中的行%x%!,像下面的开始代码,它不返回任何东西,而不是URL
start%browser%%地址%> nul 2>& 1
start% !行%x%! > nul 2>& 1
::起始码不起作用,不幸的是,这是我的问题
ping localhost -n 2> nul
goto ReadLines

:完成
::继续或任何...

所以我的问题:为什么,当回音,!行%x%!返回它应该的值,但是当
调用uppon命令,如启动%browser%%地址%,它不返回什么?
BTW你可以运行上面的代码只是把一些链接在一个txt文件名为'Test.txt',你会看到我的问题自己
如果对任何人看起来丑陋,真的只是把这一起为一个人,但后来遇到这个错误,需要为他们解决这个。

解决方案

  @ECHO OFF 
setlocal EnableDelayedExpansion
set browser = chrome.exe
set i = 0
for %% f in(q20300102.txt)do(
set i = 0
for / F delims =%% l in(%% f)do(
set / A i + = 1
set line!i!= %% l
))
::读取Test.txt的内容并将每行保存到由两个变量组成的不同变量
set x = 0
:ReadLines
如果%x%gtr%i%(
goto Completed)
set / a x + = 1
set / a odd =%x %%% 2
如果%odd%== 1(
设置地址=!行%x%!
echo!行%x%!
REM上面的echo回来的文本文件中的URL
echo%地址%
REM但是上面的echo即使%地址%已设置为!Line%x%!
REM暂停
当我尝试使用时,回来没有什么(然后是回声关闭或回声打开)!在我的代码行%x%!,像下面的开始代码,它不返回任何东西,而不是URL
ECHO开始%浏览器%%地址%
ECHO开始%浏览器%!行%x%!
REM不能启动代码工作不幸的是,这是我的ISSUE
ECHO ping localhost -n 2

goto ReadLines
:Completed
:: Continue或Whatever ...
GOTO:EOF

基本 delayedexpansion 问题。在任何复合语句(即括号语句组)中,任何%var%的出现被 var

因此,当x = 1时, odd wil设置为1,语句组将被解析, echo%Address%将有%address% address 的THEN-当前值替换,但地址尚未分配值,因为代码仅



同样,启动%browser%%地址%有一个值对于 browser ,并且该值被替换,但地址未分配, p>

在括号代码的末尾,你会返回:readlines x = 1 。这是递增的,并且 x 被分配 2 。这是不奇怪的,因为我曾经经历过,所以整个括号代码被跳过,批处理进行到以下行。在批处理中,到达标签不会结束程序,所以它只是充电和火箭到文件的末尾。



在我的代码中, ve


  1. 将文件名设置为 q20300102,txt 以适合我的系统。

  2. REM 替换了 :: 注释样式,因为在某些实现中, a broken-label(:: - comment)会导致复合语句被终止。

  3. 删除了重定向器,允许 START PING code> ECHO ed(并插入 ECHO 关键字)

  4. $ c> GOTO readlines 在化合物之外,无论 x 是否为奇都会强制循环。

由于你在Test.txt中没有举例,我自己创建了 q20300102.txt 其中包含:

  Address_one 
Address_two
Address_three
Address_four
Address_five
Address_six
Address_seven
Address_eight
Address_nine

以上,产生了

  Address_one 
ECHO已关闭。
start chrome.exe
start chrome.exe Address_one
ping localhost -n 2

Address_three
Address_one
start chrome.exe Address_one
start chrome.exe Address_three
ping localhost -n 2

Address_five
Address_three
start chrome.exe Address_three
start chrome.exe Address_five
ping localhost -n 2

Address_seven
Address_five
start chrome.exe Address_five
start chrome.exe Address_seven
ping localhost -n 2

Address_nine
Address_seven
start chrome.exe Address_seven
start chrome.exe Address_nine
ping localhost -n 2



(我已经通过迭代打破了实际输出,以便更加清楚)



所以 - 响应是



Linex内容

地址内容(注意 - 当(语句)PARSED时它的位置)

包括刚刚描述的元素的开始语句

Ping语句



所以这应该让你走上解决问题的路径...


Inside Test.txt reads a URL on the first line, theres more but for this it unimportant.

setlocal EnableDelayedExpansion
set browser=chrome.exe
set i=0
for %%f in (Test.txt) do (
    set i=0
    for /F "delims=" %%l in (%%f) do (
        set /A i+=1
        set line!i!=%%l
        ))
::the above read the contents of Test.txt and saved each line to a different Variable made up of two variables
set x=0
:ReadLines
if %x% gtr %i% (
    goto Completed)
set /a x+=1
set /a odd=%x%%%2
if %odd%==1 (
    set Address=!line%x%!
    echo !Line%x%!
    ::the above echo's back the URL from the Text File
    echo %Address%
    ::but the above here, echo's back nothing at all,(which then is either echo is off or echo is on) even though %Address% has been set to !Line%x%!
    pause
    ::when i try to use !Line%x%! in my code, like the start code below, it returns nothing instead of the URL
    start %browser% %Address% >nul 2>&1
    start %browser% !Line%x%! >nul 2>&1
    ::neither start code works unfortunately and that's my ISSUE
    ping localhost -n 2 >nul
    goto ReadLines
)
:Completed
::Continue or Whatever...

so my question: Does anyone know why, when echoed, !Line%x%! returns the values it should, but when called uppon for a command such as, start %browser% %Address%, that it returns nothing? BTW you can run the above code just put some link in a txt doc named 'Test.txt' and you'll see my issue for yourself And Sorry if it looks kind of ugly to anyone, i really just threw this together for someone but then ran into this error and need to solve it for this for them. Any Help is wanted, Thanks!

解决方案

@ECHO OFF
setlocal EnableDelayedExpansion
set browser=chrome.exe
set i=0
for %%f in (q20300102.txt) do (
    set i=0
    for /F "delims=" %%l in (%%f) do (
        set /A i+=1
        set line!i!=%%l
        ))
::the above read the contents of Test.txt and saved each line to a different Variable made up of two variables
set x=0
:ReadLines
if %x% gtr %i% (
    goto Completed)
set /a x+=1
set /a odd=%x%%%2
if %odd%==1 (
    set Address=!line%x%!
    echo !Line%x%!
    REM the above echo's back the URL from the Text File
    echo %Address%
    REM but the above here, echo's back nothing at all,(which then is either echo is off or echo is on) even though %Address% has been set to !Line%x%!
    REM pause
    REM when i try to use !Line%x%! in my code, like the start code below, it returns nothing instead of the URL
    ECHO start %browser% %Address%
    ECHO start %browser% !Line%x%!
    REM neither start code works unfortunately and that's my ISSUE
    ECHO ping localhost -n 2
)
goto ReadLines
:Completed
::Continue or Whatever...
GOTO :EOF

Fundamental delayedexpansion problem. In any compound statement (ie. a parenthesised group of statements) any occurrence of %var% is replaced by the value of var at the time the statement is PARSED, and THEN it is executed.

Consequently, when x=1, odd wil be set to 1, the statement group will be parsed and echo %Address% will have %address% replaced by the THEN-current value of address - but address is not yet assigned a value, since the code is only being PARSED - it has not yet been RUN.

Similarly, start %browser% %Address% HAS a value for browser and that value IS substituted, but address is not assigned, so is replaced by [nothing].

At the end of the parenthesised code, yo return to :readlines with x=1. This is incremented and x is assigned 2. This is not odd as far as I've ever experienced, so the entire parenthesised code is skipped and batch proceeds to the following line. In batch, reaching a label does not "end a procedure" so it simply charges on and rockets off to the end of the file.

In my code above, I've

  1. Set the filename to q20300102,txt to suit my system.
  2. Replaced the :: commenting style with REM because in some implementations, a broken-label (::-comment) causes the compound statement to be terminated.
  3. Removed the redirectors to allow the START and PING lines to be ECHOed (and inserted the ECHO keyword)
  4. Moved the GOTO readlines outside of the compound where it will force a loop regardless of whether x is odd or not.

Since you gave no example of a line in your Test.txt, I made my own q20300102.txt which contains:

Address_one
Address_two
Address_three
Address_four
Address_five
Address_six
Address_seven
Address_eight
Address_nine

and ran the above, which yielded

Address_one
ECHO is off.
start chrome.exe 
start chrome.exe Address_one
ping localhost -n 2

Address_three
Address_one
start chrome.exe Address_one
start chrome.exe Address_three
ping localhost -n 2

Address_five
Address_three
start chrome.exe Address_three
start chrome.exe Address_five
ping localhost -n 2

Address_seven
Address_five
start chrome.exe Address_five
start chrome.exe Address_seven
ping localhost -n 2

Address_nine
Address_seven
start chrome.exe Address_seven
start chrome.exe Address_nine
ping localhost -n 2

(I've broken the actual output by iteration for a little more clarity)

So - the response is

Linex content
Address content (note - as it stood when the (statement) was PARSED)
Start statements including the elements just described
Ping statement

So this should get you on the path to solving your problem...

这篇关于变量内部的批变量在调用时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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