批次:串连SETLOCAL EnableDelayedExpansion外任意两个字符串 [英] Batch: Concatenate two arbitrary strings outside SETLOCAL EnableDelayedExpansion

查看:222
本文介绍了批次:串连SETLOCAL EnableDelayedExpansion外任意两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

主要脚本延迟扩展运行的停用的,所以我必须使用的 SETLOCAL EnableDelayedExpansion 的实际串联。我只是不知道如何将结果返回到本地,进入全局变量。

我想避免使用临时文件。

我想允许的批处理文件,当地块外延迟扩展。

感谢。

编辑:

@Jeb结果
我试着用你的code内联,而不是在一个功能,它的工作。结果
然后我试图把它在一个for循环中,并把它弄坏了。结果
从循环=作品函数调用。结果
Inline是一个循环=我没有工作。结果
我现在不需要该功能。这只是一个观察。结果
谢谢你。

 关闭@echo
REM从函数调用内联执行变更
SETLOCAL EnableDelayedExpansion
CLS
FOR / F %%一个在('复制/ Z%〜dpf0NUL)也将CR = %%一个
设置LF = ^
REM两个空行是neccessary
设置原来=零* %%〜阿%%一B %%〜Ç%%〜大号LF的One&安培;!!!!!!!行LF两次与exclam ^ LF三连的报价^&安培;&放大器; !LF四连^^^^ ^!| ^< ^> ()^&安培; ^^^! ^!LF!xxxxxwith CR!CR!五个人!LF!6与^^Q ^^L依然六个一SETLOCAL DisableDelayedExpansion
SET结果=
REM电话:lfTest结果原创
::::::::::::::::::::
对/ L %% i的(1,1,2)做(
SETLOCAL
设置NotDelayedFlag =!
回声(
如果定义NotDelayedFlag(回声lfTest与延迟扩展名为禁用),否则回声lfTest被称为启用延迟扩展
SETLOCAL EnableDelayedExpansion
设置VAR =!原创!REM呼应输入为:
REM回音!无功!
回声(REM ** prepare退货
设置!VAR = VAR:%% = %%〜2!
设置VAR = VAR:!!= %%〜3
对于一个%%的(!LF)并设为VAR = VAR:!%%〜A = %%〜L
对于一个%%中(CR!)并设为VAR = VAR:!%%〜一= %%〜4REM **如预期这里有必要使用两个IF的,否则%VAR%扩大不起作用
如果没有定义NotDelayedFlag设置VAR = VAR:!^ = ^^^^
如果没有定义NotDelayedFlag设置VAR =%VAR:!= ^^^%!设置替换= %%!CR !! CR!
在%% L(!LF!)做(
   FOR / F令牌= 1,2,3%% 2(!代替!)DO(
     ENDLOCAL
     ENDLOCAL
     设置结果=%VAR%!
     关闭@echo
   )


::::::::::::::::::::
SETLOCAL EnableDelayedExpansion
回声结果有残疾延迟扩展:
如果!原创! ==!结果! (回声OK)ELSE呼应!结果!回声------------------
回音!原创!暂停
GOTO:EOF


解决方案

就像我在您的其他问题表示:<一href=\"http://stackoverflow.com/questions/9408953/batch-returning-a-value-from-a-setlocal-enabledelayedexpansion\">Batch:返回从SETLOCAL EnableDelayedExpansion

只需按照链接一个完美的解决方案。

或者,我可以在这里贴上code

  REM ** preparing CR和LF供以后使用
FOR / F %%一个在('复制/ Z%〜dpf0NUL)也将CR = %%一个
设置LF = ^
REM两个空行是neccessary

然后,在你的函数开始,以检测是否delayedExpansion为OFF或ON

  SETLOCAL
设置NotDelayedFlag =!
SETLOCAL EnableDelayedExpansion

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

 退货REM ** prepare
设置!VAR = VAR:%% = %%〜2!
设置VAR = VAR:!!= %%〜3
对于一个%%的(!LF)并设为VAR = VAR:!%%〜A = %%〜L
对于一个%%中(CR!)并设为VAR = VAR:!%%〜一= %%〜4REM **如预期这里有必要使用两个IF的,否则%VAR%扩大不起作用
如果没有定义NotDelayedFlag设置VAR = VAR:!^ = ^^^^
如果没有定义NotDelayedFlag设置VAR =%VAR:!= ^^^%!设置替换= %%!CR !! CR!
在%% L(!LF!)做(
   FOR / F令牌= 1,2,3%% 2(!代替!)DO(
     ENDLOCAL
     ENDLOCAL
     设置%〜1 =%VAR%!
     关闭@echo
      GOTO:EOF
   )

编辑:一点点变短结果
好吧,这看起来有点复杂,而且在许多情况下,它可以与其他伎俩来解决。结果
如果你知道,你会从EnableDelayed到DisableDelayed切换回来,你一定不使用任何一个LF FOR-回报将工作了。

 关闭@echo
SETLOCAL
拨打:MYTEST结果
结果集
GOTO:EOF:MYTEST
SETLOCAL EnableDelayedExpansion
REM ...做的东西在这里
设置值= ^ _&安培;!_ %% _ | _&gt;中回声 -
FOR / F ^EOL ^ = ^^ ^ delims = ^%%一中(!值!)做(
    ENDLOCAL
    集%〜1 = %%一个
    GOTO:EOF

FOR / F ^EOL的分裂^ = ^ .... 仅供禁用EOL字符。

I need to concatenate two string variables and place the result back in the first variable. These two strings can contain any arbitrary characters, like newline, exclamation, etc.

The main script runs with delayed expansion disabled, so I have to use SETLOCAL EnableDelayedExpansion for actual concatenation. I just don't know how to get the result back out of the local and into the global variable.

I would like to avoid using temporary files.

I wish batch files allowed delayed expansion outside of the local block.

Thanks.

EDIT :

@Jeb
I tried using your code inline, instead of in a function, and it worked.
Then I tried putting it in a FOR loop, and that broke it.
Function call from a loop = works.
Inline is a loop = didn't work for me.
I don't need that functionality right now. This is just an observation.
Thanks.

@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

解决方案

Like I said in your other question: Batch: Returning a value from a SETLOCAL EnableDelayedExpansion

Just follow the link for a "perfect" solution

Or I can paste the code here

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

Then at the start of your function, to detect if delayedExpansion is OFF or ON

setlocal
set "NotDelayedFlag=!"
setlocal EnableDelayedExpansion

And at the end of your function, to return the value

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
   )
)

EDIT: A little bit shorter variation
Ok, this looks a little bit complicated, and in many cases it can be solved with an other trick.
If you know, that you will switch back from an EnableDelayed to DisableDelayed and you are sure not using any LF a FOR-RETURN will work too.

@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
) 

The splitting of the for /f ^"eol^=^.... is only for disabling the eol character.

这篇关于批次:串连SETLOCAL EnableDelayedExpansion外任意两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆