Windows Command Prompt: How to pass multi-line string parameters
Asked Answered
P

3

26

I have a program that accepts a string parameter. I create a batch file that executes the program and a multiline string paramter. I also have a second parameter after the multiline string.

C:\>MyProgram "This is a
multiline text" parameter2

When I run this, only the first line of string is included in the command and the subsequent lines and the second parameter are ignored. Is there any way to pass multiline string parameters?

Purse answered 14/8, 2012 at 7:55 Comment(4)
Why do you need a multi-line?Schleiermacher
Since I am passing a string, ^ character is not possible since it will be included in the string itself. I tried it, and it is still not working.Purse
This really isn't a duplicate question, and it's completely valid. He's not asking about how to execute a multiline command, but rather how to push a string parameter that has more than one line. I'm in the same boat, I'm trying to call a program in a batch file. The ^ escapes the next line for command processing, but is passed to the program too where the syntax is invalid.Yawp
@Yawp I voted to reopen.Encephalogram
S
15

Your question is duplicate to - Windows: How to specify multiline command on command prompt?

In the Windows Command Prompt the ^ is used to escape the next character on the command line.

For example, (the More? being a prompt):

C:\>cd "c:\Program Files" ^
More? "\Common Files"

C:\>MyProgram "This is a " ^  
More? "multiline text" parameter2
Schleiermacher answered 14/8, 2012 at 8:1 Comment(9)
If it's a duplicate vote to close or, if you have less than 3,000 rep, flag the question, don't answer.Merv
Hi, I am passing a string parameter. If I add ^ character then it will be included in the input.Purse
You have to terminate the string before the ^ character? In my case that is not possible because the multiline string command is generated by another program. So if it generates two lines of string, I can only add the opening and ending quotes at the very beginning and at the very end of the string parameter.Purse
@Purse If you are accepting the parameters from another program so trim it to one line..Schleiermacher
We are using an IDE called uniPaaS which really lacks lot's of tools for string manipulation. Anyway, I think I might just find another solution like saving the actual string into a text file and passing the name of the text file instead of the string. Thanks for your answers.Purse
I agree that this is NOT duplicate. I have the same problem and it infuriates me that someone (or some people in this case) who cannot understand the problem flag it as duplicate and take away from others option to have an answer.Selfemployed
This method only works for two lines, as you can't (e.g.) use "\Common Files" ^ to continue the string any further.Branchia
This is not an answer to the OP's question. This addresses breaking lines outside of strings, rather than inside of strings, in CMD.Genip
This answer does not explain how to use a multiline string in the command promptBoulanger
N
2

This routine will write multiple lines to text file ASM.txt in the drive and directory of F:\Backup_Info. Note that it will give a line space using the space then ^ symbol as shown, a line space is required between each statement:

(echo To Do is to Understand^

Who Dares Wins^

 ^

Baz) > F:\Backup_Info\ASM.txt
Novellanovello answered 8/10, 2020 at 8:30 Comment(1)
Welcome to StackOverflow. I've added some code fences to your post so that a monospace font is used and the code is easier to recognize. You can also edit your post yourself if you still find errors.Jijib
A
0

You can save ^ 's output as a variable

set br= ^
<</br (newline)>>
<</br>>

example:

@echo off
setlocal enableExtensions enableDelayedExpansion
rem cd /D "%~dp0"
set br= ^


rem br, can't be saved to a var. by using %..%;


set "t=t1!br!t2!br!t3"

for /f "usebackq tokens=* delims=" %%q in ('!t!') do (
    echo %%q
)


:scIn
rem endlocal
pause
rem exit /b

; output:

t1
t2
t3
Press any key to continue . . .
Antoine answered 2/4, 2019 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.