batch file - How to escape "~" in set/replace command? -
i'm trying replace instances of ~ {~} in batch script vbscript doesn't assume mean hit enter when pass sendkeys, however, variable keeps getting replaced literal string meant replace it.
for example,
rem intended function; rem input = hello~world. rem output = hello{~}world. however each time, either nothing, or sets refvar literal string refvar:~={~}.
here's have tried;
set refvar=!refvar:~={~}! rem set refvar=%refvar:~={~}% rem and... set refvar=%refvar:^~=^{^~^}% rem , of course; set refvar=!refvar:^~=^{^~^}! rem , even: set "pre=~" set "post={~}" set refvar=%refvar:!pre!=!post!% rem , same set commands with; set refvar=!refvar:%pre%=%post%! am missing way escape this? i'm pretty sure has ~ being positional character in similar command.
i understand there's way fix in vbscript, bothers me have restriction without apparent workarounds me.
i offer perhaps easier solution replacing each tilde character in value string of environment variable tilde in braces dennis van gils works fine task.
@echo off set "testvar=hello~world." echo testvar has value: %testvar% call :specialreplace "testvar" echo testvar has value: %testvar% goto :eof rem subroutine below expects name of environment variable rem parameter. subroutine nothing if called without parameter. rem nothing done if specified environment variable not defined. rem each tilde character in value of environment variable replaced rem {~} subroutine. rem note: subroutine can modified replace other rem special characters equal sign different string rem can no string in case of special character should removed. rem modify search , replace variables different replace. rem aware of more code must changed if search string longer rem 1 character. length of replace string not matter on code below. :specialreplace if "%~1" == "" exit /b setlocal enabledelayedexpansion set "varname=%~1" set "varvalue=!%varname%!" if "!varvalue!" == "" endlocal & exit /b set "newvalue=" set "search=~" set "replace={~}" :replaceloop if "!varvalue:~0,1!" == "!search!" ( set "newvalue=!newvalue!!replace!" ) else ( set "newvalue=!newvalue!!varvalue:~0,1!" ) set "varvalue=!varvalue:~1!" if not "!varvalue!" == "" goto replaceloop endlocal & set "%varname%=%newvalue%" & exit /b for understanding used commands , how work, open command prompt window, execute there following commands, , read entirely pages displayed each command carefully.
call /?echo /?endlocal /?exit /?goto /?if /?rem /?set /?setlocal /?
and see answer on single line multiple commands using windows batch file understand how 2 lines multiple commands single ampersand between commands work.
a variant of above extended subroutine specialreplace supporting variable search , replace strings specified second , third parameter suggested dennis van gils.
@echo off set "testvar=hello~world." echo testvar has value: %testvar% call :specialreplace "testvar" "~" "{~}" echo testvar has value: %testvar% goto :eof rem subroutine below expects name of environment variable rem first parameter. subroutine nothing if called without parameter. rem nothing done if specified environment variable not defined. rem second parameter must search string. subroutine rem nothing if there no second parameter can 1 rem or 2 double quotes enclosed in double quotes. rem third parameter optional replace string. without third rem parameter occurrences of search string removed value rem of specified environment variable. rem 1 or 2 double quotes in double quotes search string rem no replace string can defined because command processor not rem parse quote strings expect it. """ or """" rem search string no replace string works , results in removing rem double quotes or occurrences of 2 double quotes. :specialreplace if "%~1" == "" exit /b setlocal enabledelayedexpansion set "varname=%~1" set "varvalue=!%varname%!" if "!varvalue!" == "" endlocal & exit /b set "newvalue=" set "search=%~2" if "!search!" == "" endlocal & exit /b set "replace=%~3" set "length=0" :getlength if "!search:~%length%,1!" == "" goto replaceloop set /a length+=1 goto getlength :replaceloop if "!varvalue:~0,%length%!" == "!search!" ( set "newvalue=!newvalue!!replace!" set "varvalue=!varvalue:~%length%!" ) else ( set "newvalue=!newvalue!!varvalue:~0,1!" set "varvalue=!varvalue:~1!" ) if not "!varvalue!" == "" goto replaceloop endlocal & set "%varname%=%newvalue%" & exit /b
Comments
Post a Comment