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

If I pass the text as follows:

first line second line 

In the .bat file, it takes the first line only as a parameter value.

How can i fix this? Thanks

+4
source share
2 answers

It is almost impossible to pass a new line in the batch file argument. This can be done, but I do not think that someone has developed a pragmatic way to correctly read such a parameter in a batch file.

It is best to define an environment variable containing two lines of text, including a new line. Then pass the variable name as an argument to the package, and then let the batch file access the value using a slow extension.

test.bat:

 @echo off setlocal enableDelayedExpansion echo !%1! 

From the command line:

 >set multiLine=hello^ More? More? world >test multiLine hello world 

For everyone who is interested, here is a discussion started by jeb regarding new lines in the batch parameters: http://www.dostips.com/forum/viewtopic.php?t=1768

+2
source

You can access all options, but only if batch files are launched with cmd /c , for example. like drag & drop action.

 @echo off setlocal EnableDelayedExpansion echo !cmdcmdline! 

But if you run the batch file from the prompt, this will not work, since cmdcmdline only contains how cmd.exe was run, in this case it is usually something like "C:\Windows\system32\cmd.exe" .

0
source

Source: https://habr.com/ru/post/1397781/


All Articles