Can I pass a newline in a command line parameter to a Windows executable in a batch file?

3

Say, I have an executable file that I want to pass a parameter to from a .bat file. The catch is that the parameter value must have a newline in it.

When I try the following:

"c:\folder\my app.exe" parameter="line1\nline2"

the result doesn't get interpreted as having a new line between line1 and line2.

Any ideas how to do it?

c00000fd

Posted 2017-08-24T00:41:30.723

Reputation: 339

Answers

1

No, not via batch file itself. But exe files or other programs can pass arguments containing new lines to other Exe files.

So your solution here is to create trivial tool parsing your arguments which you will call like

runner "c:\folder\my app.exe" parameter="line1\nline2"

and it will substitute \n for true LF character (here shown as ) and execute the call using P/Invoke:

"c:\folder\my app.exe" parameter="line1↵line2"

Another way to overcome this limitation is using PowerShell. It has different command line parsing.

miroxlav

Posted 2017-08-24T00:41:30.723

Reputation: 9 376

It can be done. See this answer How can you echo a newline in batch files?

– DavidPostill – 2017-08-24T09:51:48.263

@DavidPostill – Hi David, please see history of my answer. It can be done with echo, but I removed that from the answer, because it cannot be done with regular exe files and the OP does not care about echo. echo seems to be an exception. I have made many tests and searching before reverting the answer to current that it cannot be done. However, I have seen this to be done regularly by developers when going through P/Invoke. That way it works and this is what I advice in the answer. It seems that the breaking part is parsing of command line in cmd. – miroxlav – 2017-08-24T10:00:49.883

Fair enough. I don't have an exe file I could test it with. – DavidPostill – 2017-08-24T10:09:26.283

@DavidPostill – you can still test it with cmd /c echo ...... :) – miroxlav – 2017-08-24T11:10:40.767

@DavidPostill It still can be done, your first link points into the right direction. See my answer

– jeb – 2020-01-24T10:28:54.320

1

Yes, you can.
Use a small batch file to define a \n variable.

@echo off

(set \n=^^^
%=empty, do not delete this line =%
^
%=empty, do not delete this line =%
)

myApp.exe Line1%\n%Line2

It even works, if the parameter contains quotes.
The quotes has to be escaped.

myApp.exe parameter=^"aaa%\n%bbb^"

The definition of the new line looks a bit complex, but it caused by the rules of the caret

jeb

Posted 2017-08-24T00:41:30.723

Reputation: 216

0

Can't be done.

Similar to why the chown Unix command uses a colon as a separator: chown assumes usernames end with colons. Likewise, batch files have a file format.

The file format of a batch file assumes that the newline character ends a line. There is no supported escape sequence. So, what you're asking for cannot be done.

One possible workaround is if the program recognizes an escape sequence, such as recognizing \n. (JScript, supported as .js files that CScript can use, supports the unescape command. WScript.Echo(unescape("%0d0a"));)

Another common approach to handle this is if a program supports a data stream, which could be a specified filename or the "standard input" stream. Newline characters/sequences can be provided to a program that way.

Those approaches are used by multiple programs. I'm not offhand recalling any way to just have batch ignore a newline character, nor any other standardized way to try to encode that character in a way that all/most programs will recognize the character(s) that you're trying to refer to.

TOOGAM

Posted 2017-08-24T00:41:30.723

Reputation: 12 651

There is a escape character for newlines (the caret). – jeb – 2020-01-24T09:48:03.297

@jeb Thanks, I appreciate the pointer. Even though there may be an Escape character, the rest of the answer remains relevant because that Escape character does not seem to effectively make the Enter key work with nearly all programs. I'm quickly jumping to that conclusion based on minimal testing: using "echo hi^", followed by the Enter key, does cause the CMD.exe (and JP Software's TCC/LE, but not PowerShell) to act in a way suggesting the Enter key is being treated by an Escape character. However, the echo command just strips out that Enter (even if redirecting the output to a file) – TOOGAM – 2020-01-24T10:06:55.153

It works, see my answer. The batch only produces the line feed in a parameter. This works with the most apllications

– jeb – 2020-01-24T10:24:50.980

@jeb : I'm being outclassed with knowledge of the command line. Also, 'twas done with class. What a pleasant experience. Thank you. Your effort was certainly worthy of the upvote I've provided to that answer. – TOOGAM – 2020-01-24T10:31:15.390