我正在编写一个简单的脚本来替换环境变量中的文本和其他文本.我遇到的麻烦是从其他变量中提取替换或替换文本
SET a=The fat cat
ECHO %a%
REM Results in 'The fat cat'
ECHO %a:fat=thin%
REM Results in 'The thin cat'
Run Code Online (Sandbox Code Playgroud)
工作正常(输出是'肥猫'和'瘦猫'
但是,如果'fat'或'thin'在变量中,它就不起作用
SET b=fat
ECHO %a:%c%=thin%
REM _Should_ give 'The thin cat'.
REM _Actually_ gives '%a:fat=thin%' (the %c% is evaluated, but no further).
REM using delayed evaluation doesn't make any difference either
ECHO !a:%c%=thin!
REM Actual output is now '!a:fat=thin!'
Run Code Online (Sandbox Code Playgroud)
我知道这可以像以前在博客中看到的那样完成,但我从未保存过博客的链接.
有人有主意吗?
PS.我在Windows 7上运行脚本
PPS.我知道这在Perl/Python /其他脚本语言中更容易选择,但我只是想知道为什么那些应该很容易的事情并不是很明显.
购买力平价.我还尝试了明确打开延迟扩展的脚本
SETLOCAL enabledelayedexpansion
Run Code Online (Sandbox Code Playgroud)
这没什么区别.