"im passing a multi line text as argument which will be saved in a variable and then the file created has only 1 lline" Code Answer

5

obviously, there is a way to to pass a value with multi-line text in a batch file parameter.
but it is a little bit complex and currently there isn't a way to get the content out of the parameter in a safe way for any possible content.

a simple demonstration

producer.bat - puts "line1<line feed>line2" into %1

@echo off
setlocal enabledelayedexpansion
(set lf=^
%=empty=%
)
set ^"pvar=^^^^^"line1^^^^!lf!!lf!line2""
call receiver.bat %%pvar%% #

receiver.bat

@echo off
prompt :
@echo on
(
@exit /b
rem # %~1 #
)

output

:(

rem # line1
line2 #
)

to fetch this, the solution from how to receive even the strangest command line parameters? can be extended.

the problem is, that it only works with simple content, but there exists content which can't be received without producing parser errors.
like: "line1<line feed>line2"

therefore i would recommend aacini's solution

edit: improved samples

a batch file to call another batch file with multi line variables.

@echo off
setlocal enabledelayedexpansion
(set n=^
%=empty=%
)

set "var=line1!n!line2!n!line3!n!line4"
call :callbatch var
exit /b


:callbatch
setlocal enabledelayedexpansion
set "prepvar=!%~1!"
for %%l in ("!n!") do (
    for %%b in ("^=^^" "&=^&" "<=^<" ">=^>" "|=^|") do set "prepvar=!prepvar:%%~b!"
    set "prepvar=^^"!prepvar:%%~l=^^%%~l%%~l!""
)
rem echo !prepvar!-
call receiver.bat %%prepvar%%
exit /b

and a receiver.bat that accepts and parses a multi line value from %1.

@echo off
setlocal enableextensions disabledelayedexpansion
prompt :
(
    @echo on
    for %%a in (4) do (
        @goto :next
        rem # %~1#
    )
) > param.tmp
:next
@echo off
endlocal
setlocal disabledelayedexpansion
set lineno=0
(
for /f "skip=3 delims=" %%l in (param.tmp) do (
    set /a lineno+=1
    set "line=%%l"
    setlocal enabledelayedexpansion
    if !lineno! equ 1 (
        set "line=!line:*# =!"
        set "line=!line:~,-2!"
    ) else if "!line!"==") " (
        goto :exit
    ) else (
        set "line=!line:* =!"
        set "line=!line:~,-1!"
    )
    (echo(!line!)
    endlocal
)
) > file.txt
:exit

type file.txt

edit2: unsolved problems
it's possible to fetch any content in line1, but all other lines have restrictions for their content.
the content has to be "parse conform", that means that a multi line parameter with this content fails

line1
if a=

it's because the first line can be secured with rem, but all other lines have to be valid lines.
carets are only visible in line1 (with the rem technic).
and lines beginning with @ are empty and : are removed completly.

By frankybboy on January 5 2022

Answers related to “im passing a multi line text as argument which will be saved in a variable and then the file created has only 1 lline”

Only authorized users can answer the Search term. Please sign in first, or register a free account.