我正在尝试在批处理脚本中替换所有~with的实例,{~}以便VBScript不会假设我在将其传递给sendKeys时按Enter键,但是,该变量一直被替换为替换它的文字字符串.
例如,
rem Intended function;
rem input = Hello~world.
rem output = Hello{~}world.
Run Code Online (Sandbox Code Playgroud)
但是每次都没有做任何事情,或者将refVar设置为文字字符串refVar:~={~}.
这是我尝试过的;
set refVar=!refVar:~={~}!
rem Also
set refVar=%refVar:~={~}%
rem and...
set refVar=%refVar:^~=^{^~^}%
rem and of course;
set refVar=!refVar:^~=^{^~^}!
rem and even:
set "pre=~"
set "post={~}"
set refVar=%refVar:!pre!=!post!%
rem and the same set commands with;
set refVar=!refVar:%pre%=%post%!
Run Code Online (Sandbox Code Playgroud)
我错过了逃避这种方法的方法吗?我很确定它与~在类似命令中成为位置字符有关.
我知道在VBScript中可能有一种方法可以解决这个问题,但这让我很难在没有明显变通方法的情况下获得限制.
我提供了一个可能比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 The subroutine below expects the name of an environment variable as
rem parameter. The subroutine does nothing if called without parameter.
rem Also nothing is done if specified environment variable is not defined.
rem Each tilde character in value of this environment variable is replaced
rem by {~} by this subroutine.
rem Note: This subroutine can be also easily modified to replace other
rem special characters like the equal sign by a different string which
rem can be also no string in case of special character should be removed.
rem Just modify Search and Replace variables for a different replace. But
rem be aware of more code must be changed if search string is longer than
rem one character. Length of replace string does 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
Run Code Online (Sandbox Code Playgroud)
为了了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
call /?echo /?endlocal /?exit /?goto /?if /?rem /?set /?setlocal /?另请参阅使用 Windows 批处理文件的单行多个命令的答案,以了解具有多个命令的两行以及命令之间有一个 & 符号的工作原理。
上述的变体,具有扩展子例程,现在支持变量搜索和替换指定为第二个和第三个参数的字符串,如Dennis van GilsSpecialReplace建议的那样。
@echo off
set "TestVar=hello~world."
echo TestVar has value: %TestVar%
call :SpecialReplace "TestVar" "~" "{~}"
echo TestVar has value: %TestVar%
goto :EOF
rem The subroutine below expects the name of an environment variable as
rem first parameter. The subroutine does nothing if called without parameter.
rem Also nothing is done if specified environment variable is not defined.
rem The second parameter must be the search string. The subroutine does
rem nothing if there is no second parameter which can be also just one
rem or two double quotes enclosed in double quotes.
rem The third parameter is an optional replace string. Without a third
rem parameter all occurrences of search string are removed from value
rem of the specified environment variable.
rem With just one or two double quotes in double quotes as search string
rem no replace string can be defined because command processor does not
rem parse the quote strings as most would expect it. But """ or """" as
rem search string with no replace string works and results in removing
rem all double quotes or all occurrences of two 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
Run Code Online (Sandbox Code Playgroud)