Solved Newline character in DOS batch files=?

September 5, 2011 at 13:40:12
Specs: Windows 7 Ultimate, pentium 4
I want to create a variable with some text in which i want to use line breaks at one or more places. I wanted to know what character (or character combination) do we use for inserting a newline character.

See More: Newline character in DOS batch files=?

Report •


✔ Best Answer
September 7, 2011 at 00:15:12
Looks like the trick only sets LF, but not CR. The text file *does* have linebreaks, but it is unix formatted(no CR). You could use more to convert:

more lfonly.txt > crlf.txt



#1
September 5, 2011 at 14:12:32

Report •

#2
September 5, 2011 at 15:35:15
Sorry, I didn't follow your answer. Do you mean to say that we cannot do it in a batch file?
Or do you think that i should have searched for it before asking about it?

Report •

#3
September 6, 2011 at 10:34:18
You can't. End of line equates to end of command. Even if you managed to back-door a new line into the environment, variable expansion is done before command parsing.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
September 6, 2011 at 15:21:23
Thanks for explaining, Razor. No offence please at my attempt to extend our positive discussion (I know little about DOS and I am just trying to learn batch files). While searching for the answer and experimenting I came across this link.

Accordingly, batch file with the following code was created and run. It tries to dump the outputs of echo statements both on the command prompt window as well as to a file named newline.txt. Interestingly (but irritatingly enough), the output on the cmd window shows line breaks where expected, but the text redirected to newline.txt does not show up as expected.:-

setlocal EnableDelayedExpansion

REM deleting previous contents of file named newline.txt, if any
@echo off>newline.txt
@pause

REM Method1:
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
@echo There should be a newline%NL%inserted here.
@echo There should be a newline%NL%inserted here.>>newline.txt
@pause

REM method2:
@echo Line1!NLM!Line2
@echo Line1!NLM!Line2>>newline.txt
@pause

REM method3 (although without the use of a variable):
@echo Line1^

Line2
@echo Line1^

Line2>>newline.txt
@pause

And moreover, i could not understand the creation and working of the NL variable in method1. Any help would be appreciated.


Report •

#5
September 7, 2011 at 00:15:12
✔ Best Answer
Looks like the trick only sets LF, but not CR. The text file *does* have linebreaks, but it is unix formatted(no CR). You could use more to convert:

more lfonly.txt > crlf.txt


Report •

#6
September 7, 2011 at 12:30:18
Thanks Judago!

When I viewed newline.txt in Notepad++ instead of the common notepad of Windows, it was showing the output as expected.

I then checked the option View>Show symbol>Show All Characters. You are perfectly right. It shows LF characters where a CR+LF character combination was expected for a line break in case of Windows operating system (as pointed out by you)

More command also helped.

But i still could not make out the working of the method1 (as mentioned in my previous post, i got the code from this link).

Any help at demystifying the unusual NL variable assignment shall be gratefully acknowledged.


Report •


Ask Question