Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass text with new line as parameter to a bat file?

If I pass text like this:

first line
second line

to a .bat file, it is taking the first line only as a parameter value.

How can I fix this? Thanks

like image 382
Rajan Avatar asked Feb 22 '12 10:02

Rajan


People also ask

How do you make a new line in a batch file?

How can you you insert a newline from your batch file output? You can insert an invisible ascii chr(255) on a separate line which will force a blank new line. Hold down the [alt] key and press 255 on the keypad.

How do I pass a command line argument to a batch file?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

What is %% in a BAT file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do I put text in a batch file?

Open a text file, such as a Notepad or WordPad document. Add your commands, starting with @echo [off], followed by, each in a new line, title [title of your batch script], echo [first line], and pause. Save your file with the file extension BAT, for example, test. bat.


1 Answers

It is nearly impossible to pass a newline within a batch file argument. It can be done, but I don't think anyone has developed a pragmatic way to properly read such a parameter within a batch file.

Your best bet is to define an environment variable that contains the two lines of text, including the newline. Then pass the name of the variable as an argument to the batch and then let the batch file access the value using delayed expansion.

test.bat:

@echo off
setlocal enableDelayedExpansion
echo !%1!

From the command line:

>set multiLine=hello^
More?
More? world

>test multiLine
hello
world

For anyone interested, here is a discussion initiated by jeb concerning newlines in batch parameters: http://www.dostips.com/forum/viewtopic.php?t=1768

like image 187
dbenham Avatar answered Oct 19 '22 07:10

dbenham