Package: combine two arbitrary strings outside of SETLOCAL EnableDelayedExpansion

I need to combine two string variables and put the result back in the first variable. These two lines can contain any arbitrary characters, such as a newline character, exclamation point, etc.

The main script works with the extension disabled, so for the actual concatenation I have to use SETLOCAL EnableDelayedExpansion. I just don't know how to return the result from a local and global variable.

I would like to avoid using temporary files.

I want batch files to allow extension delay outside the local block.

Thank.

EDIT:

@Jeb
I tried using your inline code, not in a function, and it worked.
Then I tried to put it in a FOR loop, and that broke it.
Calling a function from a loop = works.
Inline - this loop = doesn't work for me.
I donโ€™t need this functionality right now. This is just an observation.
Thank.

@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 
+3
variables string batch-file
Feb 23 '12 at 18:27
source share
1 answer

As I said in your other question: Batch: return value from SETLOCAL EnableDelayedExpansion

Just follow the link for the โ€œ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 beginning of your function, to detect if delayedExpansion is turned 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 slightly shorter change
Well, that looks a little more complicated, and in many cases it can be solved with another trick.
If you know that you will switch from EnableDelayed to DisableDelayed, and you are sure that you are not using LF, 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 separation of for /f ^"eol^=^.... is only to disable the eol character.

+2
Feb 23 '12 at 18:46
source share



All Articles