Batch file 循环内的字符串替换
我在windows中遇到有关批处理的问题。以下代码可以在bat文件中运行良好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
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命令中使用它。