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

Batch file 循环内的字符串替换

Batch file 循环内的字符串替换,batch-file,Batch File,我在windows中遇到有关批处理的问题。以下代码可以在bat文件中运行良好 set word1= set word2= set str=I0338092020061201B.zip.Z call set str=%%str:2006=%word1%%% call set str=%%str:B.zip.Z=%word2%%% echo %str% 但问题是,当代码嵌入到循环中时,参数str返回null set word1= set word2= For /R %cd%\FX\ %%G IN

我在windows中遇到有关批处理的问题。以下代码可以在bat文件中运行良好

set word1=
set word2=
set str=I0338092020061201B.zip.Z
call set str=%%str:2006=%word1%%%
call set str=%%str:B.zip.Z=%word2%%%
echo %str%
但问题是,当代码嵌入到循环中时,参数str返回null

set word1=
set word2=
For /R %cd%\FX\ %%G IN (*.txt) do (
    Echo "%%G"
    set str=IXXXXX2020061201B.zip
    call set str=%%str:2006=%word1%%%
    call set str=%%str:B.zip=%word2%%%
    echo %str%
    timeout 1
)

有没有什么方法可以涵盖这一点,以及为什么会发生这种情况。非常感谢。

我找到了答案,解决方案如下:

setlocal enabledelayedexpansion
For /R %cd%\FX\ %%G IN (*.txt) do (
set tmp=%%G
set tmp=!tmp:%cd%\FX\200612=!
set tmp=!tmp:.txt=!
echo %%G
echo !str!
)
endlocal

谢谢

您的呼叫代码很好,只是您没有在最后一个echo命令中使用它。