Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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

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_Nested - Fatal编程技术网

Variables 嵌套到批处理变量中的批处理变量

Variables 嵌套到批处理变量中的批处理变量,variables,batch-file,nested,Variables,Batch File,Nested,根据mypath变量中“X”之后和“openfile”之前的文本,我想调用一个函数,将此文本作为参数传递,以便用“foo”替换它。为了做到这一点,我写了 @echo off SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file if /i %1=="Dashboard" goto label %1 if /i %1=="Dboard" goto label %1 :label SET mypath=%mypath:\%1\=f

根据mypath变量中“X”之后和“openfile”之前的文本,我想调用一个函数,将此文本作为参数传递,以便用“foo”替换它。为了做到这一点,我写了

@echo off
SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file
if /i %1=="Dashboard" goto label %1
if /i %1=="Dboard" goto label %1

:label 
SET mypath=%mypath:\%1\=foo%
ECHO %mypath%
我注意到,在脚本末尾回显mypath会输出

Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file  instead of 
Y:\SUT\ISTQB\Board Airport\X\foo\Open file
我认为问题是关于“SET mypath=%mypath:\%1\=foo%”中的参数%1,但我不明白为什么

事实上,我绝对需要使用参数%1,因为当前输入mypath变量的文本不是静态文本。它可以是Dboard,或者任何东西

有人能给我解释一下吗?先谢谢你

        @echo off
        SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

        if /i "%~1"=="Dashboard" goto :label 
        if /i "%~1"=="Dboard" goto :label 

        goto :eof
        :label
        rem set temp_var=%~1
        rem call SET mypath=%%mypath:%temp_var%=foo%%
        rem echo %mypath%
   setlocal enabledelayedexpansion 
        set mypath=!mypath:%~1=foo!
        ECHO !mypath!
    endlocal
你能试试这个吗


您能试试这个吗?

对于带有变量的字符串操作,您需要延迟扩展:

…输出为:

Y:\SUT\ISTQB\Board Airport\XfooOpen file

对于带有变量的字符串操作,需要延迟展开:

…输出为:

Y:\SUT\ISTQB\Board Airport\XfooOpen file

您的问题是使用%1进行搜索/替换:- SET-mypath=%mypath:\%1\=foo%包含的%字符太多,语法有点混乱

这个答案是使用EnableDelayedExpansion来利用!语法,但请注意%mypath%变量将在endlocal之后丢失其值-它只将其值保留在setlocal块中

解决此问题的一种方法是将SET命令写入临时批处理文件,并在本地块之后调用它:

setlocal EnableDelayedExpansion
  SET mypath=!mypath:%1=foo!
     rem  Create a batch file to perform SET later : 
  echo @SET mypath=%mypath% > "%TEMP%\setmyvar.bat"
endlocal

   rem  Now use the batch file to set the variable :
call "%TEMP%\setmyvar.bat"
ECHO %mypath%

如果有更优雅的方法,请告诉我们:

您的问题是使用%1进行搜索/替换:- SET-mypath=%mypath:\%1\=foo%包含的%字符太多,语法有点混乱

这个答案是使用EnableDelayedExpansion来利用!语法,但请注意%mypath%变量将在endlocal之后丢失其值-它只将其值保留在setlocal块中

解决此问题的一种方法是将SET命令写入临时批处理文件,并在本地块之后调用它:

setlocal EnableDelayedExpansion
  SET mypath=!mypath:%1=foo!
     rem  Create a batch file to perform SET later : 
  echo @SET mypath=%mypath% > "%TEMP%\setmyvar.bat"
endlocal

   rem  Now use the batch file to set the variable :
call "%TEMP%\setmyvar.bat"
ECHO %mypath%

如果有更优雅的方法,请告诉我们:

对不起。有时我觉得我不太懂这些问题,也许是因为我的母语不是英语

在您的问题中,您说过要替换mypath变量中“X”之后和“Open file”之前的文本。。。通过'foo'甚至你说的文本,当前被仪表板放入mypath变量中的文本不是静态文本。它可以是Dboard,或者任何东西,但是在您的代码和答案中的后面的注释中,似乎您只想通过foo更改strings仪表板或Dboard。这使我困惑

无论如何,这是我对你问题的解决方案,而不是你的评论

@echo off
setlocal EnableDelayedExpansion

SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

rem Get the text after 'X' and before 'Open file'
set auxPath=%mypath%
set enclosed=
set text=
:nextPart
   for /F "tokens=1* delims=\" %%a in ("%auxPath%") do (
      if "%%a" equ "Open file" (set enclosed=) else (
      if defined enclosed (set "text=!text!\%%a") else (
      if "%%a" equ "X" (set enclosed=1)
      ))
      set "auxPath=%%b"
   )
if defined auxPath goto nextPart
set text=%text:~1%

rem Replace that text by "foo"
call :Change "%text%"
goto :EOF

:Change
SET mypath=!mypath:\%~1\=\foo\!
ECHO %mypath%
exit /B
例如,上一个程序的输出如下:

Y:\SUT\ISTQB\Board Airport\X\foo\Open file
但如果mypath变量为:

Y:\SUT\ISTQB\Board Airport\X\One\Two three\Open file
Y:\SUT\ISTQB\Board Airport\X\foo\Open file
。。。输出结果如下:

Y:\SUT\ISTQB\Board Airport\X\One\Two three\Open file
Y:\SUT\ISTQB\Board Airport\X\foo\Open file

。。。这正好解决了你的要求。

对不起。有时我觉得我不太懂这些问题,也许是因为我的母语不是英语

在您的问题中,您说过要替换mypath变量中“X”之后和“Open file”之前的文本。。。通过'foo'甚至你说的文本,当前被仪表板放入mypath变量中的文本不是静态文本。它可以是Dboard,或者任何东西,但是在您的代码和答案中的后面的注释中,似乎您只想通过foo更改strings仪表板或Dboard。这使我困惑

无论如何,这是我对你问题的解决方案,而不是你的评论

@echo off
setlocal EnableDelayedExpansion

SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

rem Get the text after 'X' and before 'Open file'
set auxPath=%mypath%
set enclosed=
set text=
:nextPart
   for /F "tokens=1* delims=\" %%a in ("%auxPath%") do (
      if "%%a" equ "Open file" (set enclosed=) else (
      if defined enclosed (set "text=!text!\%%a") else (
      if "%%a" equ "X" (set enclosed=1)
      ))
      set "auxPath=%%b"
   )
if defined auxPath goto nextPart
set text=%text:~1%

rem Replace that text by "foo"
call :Change "%text%"
goto :EOF

:Change
SET mypath=!mypath:\%~1\=\foo\!
ECHO %mypath%
exit /B
例如,上一个程序的输出如下:

Y:\SUT\ISTQB\Board Airport\X\foo\Open file
但如果mypath变量为:

Y:\SUT\ISTQB\Board Airport\X\One\Two three\Open file
Y:\SUT\ISTQB\Board Airport\X\foo\Open file
。。。输出结果如下:

Y:\SUT\ISTQB\Board Airport\X\One\Two three\Open file
Y:\SUT\ISTQB\Board Airport\X\foo\Open file


。。。这正好解决了您的请求。

有趣的问题:我们有40分钟的时间,并在同一分钟内发布:-我尝试了,但它不会改变问题!好的。我在代码中做了更多的更改。只有当参数是Dashboard时,这才有效,因为mypath的初始值中没有Dboard。您也可以尝试注释的部分问题仍然存在。mypath的值不会更改!然后设置mypath=!mypath:Dashboard=foo。。。它将被更改,但您将错过DboardIntresting问题的案例:我们有40'的时间,并在同一分钟发布:-我尝试了,但它不会更改问题!好的。我在代码中做了更多的更改。只有当参数是Dashboard时,这才有效,因为mypath的初始值中没有Dboard。您也可以尝试注释的部分问题仍然存在。mypath的值不会更改!然后设置mypath=!mypath:Dashboard=foo。。。它将被更改,但您将错过dboardodoing myfile.bat Dboard的案例我的输出是Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open文件,而不是像您一样的我;这是很正常的。你想用FOO替换仪表板,但如果你通过DBOARD,什么都不会被替换。是的,如果你想替换DBOARD,你必须搜索DBOARD,如果搜索失败,什么都不能被替换。我太傻了!这很好,是因为缺乏警惕。非常感谢你。没有一个答案适用于C:\Documents&Settings\Dboard\file这样的路径。因此,最好将SET命令SET mypath=括起来!mypath:\%~1\=foo!在我的黑板上执行myfile.bat Dboard
输出为Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open文件,而不是像您一样的me;这是很正常的。你想用FOO替换仪表板,但如果你通过DBOARD,什么都不会被替换。是的,如果你想替换DBOARD,你必须搜索DBOARD,如果搜索失败,什么都不能被替换。我太傻了!这很好,是因为缺乏警惕。非常感谢你。没有一个答案适用于C:\Documents&Settings\Dboard\file这样的路径。因此,最好将SET命令SET mypath=括起来!mypath:\%~1\=foo!有太多的%字符,使语法有点混乱?天啊!你一定看到了我的一些非常令人沮丧的代码!有太多的%字符,使语法有点混乱?天啊!你一定看到了我的一些非常令人沮丧的代码+1,除了一点外,最好的答案是:OP写的文本在X之后,而不是在X\:@endro:Yes。在问题的上下文中,似乎X和Open文件字符串都应该由左斜杠字符和替换的foo字符串分隔。OP的预期输出证实了这一点:Y:\SUT\ISTQB\Board Airport\X\foo\Open文件,而其他答案中的任何一个都没有解决该问题…+1,最佳答案除了一点:OP在X之后而不是在X\之后写入文本:@endro:Yes。在问题的上下文中,似乎X和Open文件字符串都应该由左斜杠字符和替换的foo字符串分隔。OP的预期输出证实了这一点:Y:\SUT\ISTQB\Board Airport\X\foo\Open文件,而其他答案中的任何一个都没有提到该文件。。。