How to substitute variable contents in a Windows batch file

前端 未结 7 1663
栀梦
栀梦 2021-01-08 00:55

I\'m writing a simple script to substitute text in an environment variable with other text. The trouble I get is with the substituted or substituted text being pulled from o

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-08 01:41

    :: Use perl on %var% =~ s/$old/$new/

    set var=The fat cat
    set old=fat
    set new=thin
    
    ECHO before=%var%
    
    for /f "delims=" %%m in ('echo %var% ^|perl -pe "s/%old%/%new%/" ') do set var=%%m
    
    ECHO after=%var% 
    

    Output:

    before=The fat cat
    after=The thin cat
    

提交回复
热议问题