Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 变量中的批处理变量在调用时不起作用_Variables_Batch File_Cmd_Double - Fatal编程技术网

Variables 变量中的批处理变量在调用时不起作用

Variables 变量中的批处理变量在调用时不起作用,variables,batch-file,cmd,double,Variables,Batch File,Cmd,Double,Inside Test.txt读取第一行的URL,还有更多内容,但对于这一点,它并不重要 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 abo

Inside Test.txt读取第一行的URL,还有更多内容,但对于这一点,它并不重要

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...
所以我的问题是:有人知道为什么,当回声响起时!行%x%!返回它应该返回的值,但是 调用uppon命令,例如启动%browser%%Address%,但该命令不返回任何内容? 顺便说一句,你可以运行上面的代码,只需在一个名为“Test.txt”的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
        ))
::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
基本的
延迟扩展问题。在任何复合语句(即括号内的语句组)中,
%var%
的任何出现在解析语句时都会被
var
的值替换,然后执行

因此,当x=1时,
odd
将被设置为1,语句组将被解析,
echo%Address%
将有
%Address%
替换为当时的
Address
的当前值,但
Address
尚未分配值,因为代码只是在解析中,尚未运行

类似地,
start%browser%%Address%
也有一个用于
browser
的值,该值被替换,但
Address
未赋值,因此被[nothing]替换

在括号中的代码末尾,返回到
:readlines
,其中
x=1
。这是递增的,并且
x
被分配
2
。就我所经历的情况而言,这并不奇怪,因此跳过了整个括号中的代码,然后批处理继续到下一行。在批处理中,到达一个标签并不会“结束一个过程”,所以它只是充电并发射到文件的末尾

在上面的代码中,我

  • 将文件名设置为
    q20300102,txt
    ,以适合我的系统
  • ::
    注释样式替换为
    REM
    ,因为在某些实现中,破损的标签(:-comment)会导致复合语句终止
  • 删除重定向器,以允许
    开始
    PING
    回音
    插入(并插入
    回音
    关键字)
  • 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 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
    
    (为了更清楚一点,我已经通过迭代打破了实际输出)

    因此,答案是

    Linex内容
    地址内容(注意-解析(语句)时的状态)
    开始语句,包括刚才描述的元素
    平声明

    所以这会让你走上解决问题的道路

    基本的
    延迟扩展问题。在任何复合语句(即括号内的语句组)中,
    %var%
    的任何出现在解析语句时都会被
    var
    的值替换,然后执行

    因此,当x=1时,
    odd
    将被设置为1,语句组将被解析,
    echo%Address%
    将有
    %Address%
    替换为当时的
    Address
    的当前值,但
    Address
    尚未分配值,因为代码只是在解析中,尚未运行

    类似地,
    start%browser%%Address%
    也有一个用于
    browser
    的值,该值被替换,但
    Address
    未赋值,因此被[nothing]替换

    在括号中的代码末尾,返回到
    :readlines
    ,其中
    x=1
    。这是递增的,并且
    x
    被分配
    2
    。就我所经历的情况而言,这并不奇怪,因此跳过了整个括号中的代码,然后批处理继续到下一行。在批处理中,到达一个标签并不会“结束一个过程”,所以它只是充电并发射到文件的末尾

    在上面的代码中,我

  • 将文件名设置为
    q20300102,txt
    ,以适合我的系统
  • ::
    注释样式替换为
    REM
    ,因为在某些实现中,破损的标签(:-comment)会导致复合语句终止
  • 删除重定向器,以允许
    开始
    PING
    回音
    插入(并插入
    回音
    关键字)
  • 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 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
    
    (为了更清楚一点,我已经通过迭代打破了实际输出)

    因此,答案是

    Linex内容
    地址内容(注意-解析(语句)时的状态)
    开始语句,包括刚才描述的元素
    平声明

    因此,这将使您走上解决问题的道路…

    简而言之:“您需要使用延迟展开,以便获得在块内修改的变量的值”;这是批处理文件中的基本变量管理行为:

    if %odd%==1 (
        set Address=!line%x%!        <- "Address" variable was modified inside this block
    
        - - - - - -
    
        echo %Address%               <- This not work, you need Delayed Expansion here:
        echo !Address!
    
        - - - - - -
    
    )
    
    如果%odd%==1(
    set Address=!line%x%!简而言之:“您需要使用延迟扩展来获取在块内修改的变量的值”;这是批处理文件中的基本变量管理行为:

    if %odd%==1 (
        set Address=!line%x%!        <- "Address" variable was modified inside this block
    
        - - - - - -
    
        echo %Address%               <- This not work, you need Delayed Expansion here:
        echo !Address!
    
        - - - - - -
    
    )
    
    如果%odd%==1(
    
    设置地址=!行%x%!1-除非您打算迭代一组文件,否则您的
    %%f
    循环将不必要。如果