gpt4 book ai didi

batch-file - 如何在设置/替换命令中转义 "~"?

转载 作者:行者123 更新时间:2023-12-03 17:52:37 24 4
gpt4 key购买 nike

我正在尝试替换 ~ 的所有实例与 {~}在批处理脚本中,以便 VBScript 在将其传递给 sendKeys 时不会假设我的意思是按 Enter,但是,该变量不断被替换它的文字字符串替换。

例如,

rem Intended function;
rem input = Hello~world.
rem output = Hello{~}world.

然而,每次它要么什么都不做,要么将 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%!

我错过了逃避这个的方法吗?我很确定它与 ~ 有关作为类似命令中的位置字符。

我知道在 VBScript 中可能有一种方法可以解决这个问题,但这让我感到困扰,因为它对我没有明显的解决方法。

最佳答案

所以我做了一些挖掘,看起来这真的,真的很难。但是,我确实找到了 this ,它解决了这个问题,给我们这个代码:

@echo off

set "input=Hello~world."
echo input: %input%

call :wavereplacer "%input%" {tilde} output
set output=%output:{tilde}={~}%
echo output: %output%
pause

::your own code should be above this.
goto :eof

:wavereplacer String Replacer [RtnVar]
setlocal
rem the result of the operation will be stored here
set "result=#%~1#"
set "replacer=%~2"
call :strlen0.3 result wl
call :strlen0.3 replacer rl

:start

set "part1="
set "part2="

rem splitting the string on two parts
for /f "tokens=1* delims=~" %%w in ("%result%") do (
set "part1=%%w"
set "part2=%%x"
)

rem calculating the count replace strings we should use
call :strlen0.3 part1 p1l
call :strlen0.3 part2 p2l
set /a iteration_end=wl-p1l-p2l

rem creating a sequence with replaced strings
setlocal enableDelayedExpansion
set "sequence="
for /l %%i in (1,1,%iteration_end%) do (
set sequence=!sequence!%replacer%
)
endlocal & set "sequence=%sequence%"

rem adjust the string length
set /a wl=wl+iteration_end*(rl-1)

rem replacing for the current iteration
set result=%part1%%sequence%%part2%
rem if the second part is empty the task is over
if "%part2%" equ "" (
set result=%result:~1,-1%
goto :endloop
)


goto :start

:endloop
endlocal & if "%~3" neq "" (set %~3=%result%) else echo %result%
exit /b

:strlen0.3 StrVar [RtnVar]
setlocal EnableDelayedExpansion
set "s=#!%~1!"
set "len=0"
for %%A in (2187 729 243 81 27 9 3 1) do (
set /A mod=2*%%A
for %%Z in (!mod!) do (
if "!s:~%%Z,1!" neq "" (
set /a "len+=%%Z"
set "s=!s:~%%Z!"

) else (
if "!s:~%%A,1!" neq "" (
set /a "len+=%%A"
set "s=!s:~%%A!"
)
)
)
)
endlocal & if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b

这不能代替 ~{~}立即,您需要使用临时替换,在这种情况下 {tilde}

关于batch-file - 如何在设置/替换命令中转义 "~"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35245802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com