或者更复杂一点:
@echo off &setlocal enabledelayedexpansion
set "str=~/work/my_folder -param one ~ two:~"
set "replacement=C:/Users/login"
call :strlen str len
set /a len-=1
for /l %%i in (0,1,%len%) do if "!str:~%%i,1!"=="~" (set "new=!new!%replacement%") else set "new=!new!!str:~%%i,1!"
echo %new%
goto :eof
:strlen
:: list string length up to 8189 (and reports 8189 for any string longer than 8189)
:: function from http://ss64.org/viewtopic.php?pid=6478#p6478
( setlocal enabledelayedexpansion & set /a "}=0"
if "%~1" neq "" if defined %~1 (
for %%# in (4096 2048 1024 512 256 128 64 32 16) do (
if "!%~1:~%%#,1!" neq "" set "%~1=!%~1:~%%#!" & set /a "}+=%%#"
)
set "%~1=!%~1!0FEDCBA9876543211" & set /a "}+=0x!%~1:~32,1!!%~1:~16,1!"
)
)
endlocal & set /a "%~2=%}%" & exit /b
endlocal
输出:
C:/Users/login/work/my_folder -param one C:/Users/login two:C:/Users/login
这不适用于感叹号。