gpt4 book ai didi

string - 批处理 : Concatenate two arbitrary strings outside SETLOCAL EnableDelayedExpansion

转载 作者:行者123 更新时间:2023-12-03 21:26:36 24 4
gpt4 key购买 nike

我需要连接两个字符串变量并将结果放回第一个变量中。这两个字符串可以包含任何任意字符,如换行符、感叹号等。

主脚本在延迟扩展禁用的情况下运行,因此我必须使用SETLOCAL EnableDelayedExpansion 进行实际连接。我只是不知道如何将结果从本地返回到全局变量。

我想避免使用临时文件。

我希望批处理文件允许延迟扩展到本地 block 之外。

谢谢。

编辑:

@杰布
我试过使用你的内联代码,而不是在函数中,它成功了。
然后我试着把它放在一个 FOR 循环中,但它坏了。
来自循环的函数调用 = 有效。
内联是一个循环 = 对我不起作用。
我现在不需要那个功能。这只是一个观察。
谢谢。

@echo off
REM Changed from function call to inline implementation
setlocal EnableDelayedExpansion
cls
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set LF=^


rem TWO Empty lines are neccessary
set "original=zero*? %%~A%%~B%%~C%%~L!LF!one&line!LF!two with exclam^! !LF!three with "quotes^&"&"!LF!four with ^^^^ ^| ^< ^> ( ) ^& ^^^! ^"!LF!xxxxxwith CR!CR!five !LF!six with ^"^"Q ^"^"L still six "

setlocal DisableDelayedExpansion
SET result=""
REM call :lfTest result original
::::::::::::::::::::
for /L %%i in (1,1,2) do (
setlocal
set "NotDelayedFlag=!"
echo(
if defined NotDelayedFlag (echo lfTest was called with Delayed Expansion DISABLED) else echo lfTest was called with Delayed Expansion ENABLED
setlocal EnableDelayedExpansion
set "var=!original!"

rem echo the input is:
rem echo !var!
echo(

rem ** Prepare for return
set "var=!var:%%=%%~2!"
set "var=!var:"=%%~3!"
for %%a in ("!LF!") do set "var=!var:%%~a=%%~L!"
for %%a in ("!CR!") do set "var=!var:%%~a=%%~4!"

rem ** It is neccessary to use two IF's, else the %var% expansion doesn't work as expected
if not defined NotDelayedFlag set "var=!var:^=^^^^!"
if not defined NotDelayedFlag set "var=%var:!=^^^!%" !

set "replace=%% """ !CR!!CR!"
for %%L in ("!LF!") do (
for /F "tokens=1,2,3" %%2 in ("!replace!") DO (
ENDLOCAL
ENDLOCAL
set "result=%var%" !
@echo off
)
)
)
::::::::::::::::::::
setlocal EnableDelayedExpansion
echo The result with disabled delayed expansion is:
if !original! == !result! (echo OK) ELSE echo !result!

echo ------------------
echo !original!

pause
goto :eof

最佳答案

正如我在您的其他问题中所说:Batch: Returning a value from a SETLOCAL EnableDelayedExpansion

只需点击“完美”解决方案的链接

或者我可以把代码贴在这里

rem ** Preparing CR and LF for later use
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set LF=^


rem TWO Empty lines are neccessary

然后在你的函数开始时,检测delayedExpansion是OFF还是ON

setlocal
set "NotDelayedFlag=!"
setlocal EnableDelayedExpansion

在你的函数结束时,返回值

rem ** Prepare for return
set "var=!var:%%=%%~2!"
set "var=!var:"=%%~3!"
for %%a in ("!LF!") do set "var=!var:%%~a=%%~L!"
for %%a in ("!CR!") do set "var=!var:%%~a=%%~4!"

rem ** It is neccessary to use two IF's, else the %var% expansion doesn't work as expected
if not defined NotDelayedFlag set "var=!var:^=^^^^!"
if not defined NotDelayedFlag set "var=%var:!=^^^!%" !

set "replace=%% """ !CR!!CR!"
for %%L in ("!LF!") do (
for /F "tokens=1,2,3" %%2 in ("!replace!") DO (
ENDLOCAL
ENDLOCAL
set "%~1=%var%" !
@echo off
goto :eof
)
)

编辑:稍微短一点的变化
好吧,这看起来有点复杂,在很多情况下可以用其他技巧解决。
如果您知道,您将从 EnableDelayed 切换回 DisableDelayed,并且您确定不使用任何 LF,FOR-RETURN 也可以。

@echo off
setlocal
call :myTest result
set result
goto :eof

:myTest
setlocal EnableDelayedExpansion
rem ... do something here
set "value=^!_&_%%_|_>"

echo --
for /f ^"eol^=^

^ delims^=^" %%a in ("!value!") do (
endlocal
set "%~1=%%a"
goto :eof
)

for/f ^"eol^=^.... 的拆分只是为了禁用 eol 字符。

关于string - 批处理 : Concatenate two arbitrary strings outside SETLOCAL EnableDelayedExpansion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418946/

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