Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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
Batch file 循环和递归中的字符串替换具有意外的副作用_Batch File_Replace_Windows 7 - Fatal编程技术网

Batch file 循环和递归中的字符串替换具有意外的副作用

Batch file 循环和递归中的字符串替换具有意外的副作用,batch-file,replace,windows-7,Batch File,Replace,Windows 7,我试图创建一个程序,在其中我浏览文件(在一个文件夹及其所有子文件夹中),并用其他部分动态替换一行的部分。为此,我制作了一个程序,但发现它的行为出人意料 整条线都被替换了,而不仅仅是更换了绳子。然后我做了一些实验,发现在循环外,字符串替换的行为与预期的一样,但在循环内……奇怪开始了 下面是一个非常基本的例子,在一个循环中有一个普通的字符串替换 @echo off call :Rekursiv goto :eof :Rekursiv set str=teh cat in teh ha

我试图创建一个程序,在其中我浏览文件(在一个文件夹及其所有子文件夹中),并用其他部分动态替换一行的部分。为此,我制作了一个程序,但发现它的行为出人意料

整条线都被替换了,而不仅仅是更换了绳子。然后我做了一些实验,发现在循环外,字符串替换的行为与预期的一样,但在循环内……奇怪开始了

下面是一个非常基本的例子,在一个循环中有一个普通的字符串替换

@echo off   
call :Rekursiv
goto :eof

:Rekursiv
    set str=teh cat in teh hat
    echo "Outside"
    echo %str%
    set str=%str:teh=the%
    echo %str%  
    echo "Outside end"

    for /D %%d in (*) do (
            cd %%~fd

            set str=teh cat in teh hat
            echo %str%
            set str=%str:teh=the%
            echo %str%      

            call :Rekursiv      

            cd..
    )

    exit /b     
现在我通常会期望:

teh cat in teh hat
the cat in the hat
一些外面和外面的东西散落在中间,但我得到的是:

"Outside"
teh cat in teh hat        <--- as expected
the cat in the hat        <--- as expected
"Outside end"
the cat in the hat        <--- Completely unexpected as its the replaced string already!
the cat in the hat
"Outside"
teh cat in teh hat
the cat in the hat
"Outside end"
the cat in the hat
the cat in the hat
"Outside"
teh cat in teh hat
the cat in the hat
"Outside end"
the cat in the hat
the cat in the hat
"Outside"
teh cat in teh hat
the cat in the hat
"Outside end"

“echo%test%”的第一次出现在“

之后,第二次出现在“

短:解析时,在执行块之前完成百分比扩展

在内部执行任何操作之前,可以看到带有固定字符串的块

因此存在延迟扩展,在执行单个命令时对其进行计算。
语法几乎相同,但使用感叹号代替百分号。
但在使用延迟扩展之前,必须使用

setlocal EnableDelayedExpansion
所以你的代码看起来像

@echo off   
setlocal EnableDelayedExpansion
call :Rekursiv
goto :eof

:Rekursiv
    set str=teh cat in teh hat
    echo "Outside"
    echo %str%
    set str=%str:teh=the%
    echo %str%  
    echo "Outside end"

    for /D %%d in (*) do (
            cd %%~fd

            set "str=teh cat in teh hat"
            echo !str!
            set str=!str:teh=the!
            echo !str!
    )

短:解析时,在执行块之前完成百分比扩展

在内部执行任何操作之前,可以看到带有固定字符串的块

因此存在延迟扩展,在执行单个命令时对其进行计算。
语法几乎相同,但使用感叹号代替百分号。
但在使用延迟扩展之前,必须使用

setlocal EnableDelayedExpansion
所以你的代码看起来像

@echo off   
setlocal EnableDelayedExpansion
call :Rekursiv
goto :eof

:Rekursiv
    set str=teh cat in teh hat
    echo "Outside"
    echo %str%
    set str=%str:teh=the%
    echo %str%  
    echo "Outside end"

    for /D %%d in (*) do (
            cd %%~fd

            set "str=teh cat in teh hat"
            echo !str!
            set str=!str:teh=the!
            echo !str!
    )

也许可以help@MCND嗯,有趣的一点。我明白了。但在循环中,它是“之后”的,你知道为什么会有吗?(“before”我希望在循环外是一样的,但在循环内是一样的)。时刻会更新那里的解决方案。啊,也许我能理解。是否可以这样:第一次发生在if设置生效之前,因此第二次回波设置为之后?(当它在查看之前的“test=before”之前解析第二个%test%时?)help@MCND嗯,有趣的一点。我明白了。但在循环中,它是“之后”的,你知道为什么会有吗?(“before”我希望在循环外是一样的,但在循环内是一样的)。时刻会更新那里的解决方案。啊,也许我能理解。是否可以这样:第一次发生在if设置生效之前,因此第二次回波设置为之后?(当它在查看之前的“test=before”之前解析第二个%test%时?)