Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:49:11+00:00 2026-05-30T12:49:11+00:00

I need to concatenate two string variables and place the result back in the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:49:12+00:00Added an answer on May 30, 2026 at 12:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to concatenate two const chars like these: const char *one = Hello
I need to concatenate two string in another one without their intersection (in terms
It's very pointless and troublesome that everytime that you need to concatenate two strings
I have table that has two columns and I need to concatenate these two
How can I concatenate two stringstreams? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream>
I'm stuck at yet another C problem. How can I concatenate two strings with
I need to concatenate two string and display with single text view. That string
I need to display the results of either one or two strings. Each string
I need to concatenate different lines in a string. To do so, I need
I have a situation where I need to concatenate several string to form an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.