11

The following is the batch script i have written

@echo off
setlocal enabledelayedexpansion
set finalcontent=
For /F "tokens=1-2* delims=  " %%I in (abc.txt) do (
IF %%J EQU MAJORVER (
set currentline=%%I %%J %1
set finalcontent=!finalcontent!!currentline!
) ELSE IF %%J EQU MINORVER (
set currentline=%%I %%J %2
set finalcontent=!finalcontent!!currentline!
) ELSE IF %%J EQU BUILDNUM (
set currentline=%%I %%J %3
set finalcontent=!finalcontent!!currentline!
) ELSE (
set currentline=%%I %%J %%K%NL%
set finalcontent=!finalcontent!!currentline!
)
)
echo %finalcontent%>>xyz.txt

I want a newline character appended at the end of every occurence of the variable currentline. Can anyone guide me on this?

Joey
  • 344,408
  • 85
  • 689
  • 683
Rahul
  • 111
  • 1
  • 1
  • 4

2 Answers2

28

You can create a real newline character and assign it to a variable.

setlocal EnableDelayedExpansion
set LF=^


rem TWO empty lines are required
echo This text!LF!uses two lines

The newline best works with delayed expansion, you can also use it with the percent expansion, but then it's a bit more complex.

set LF=^


rem TWO empty lines are required
echo This text^%LF%%LF%uses two lines
echo This also^

uses two lines

How it works?
The caret is an escape character, it escapes the next character and itself is removed.
But if the next character is a linefeed the linefeed is also removed and only the next character is effectivly escaped (even if this is also an linefeed).

Therefore, two empty lines are required, LF1 is ignored LF2 is escaped and LF3 is neccessary to finish the "line".

set myLinefeed=^<LF1>
<LF2>
<LF3>

Hints:
It's often better to use a quite different format of the newline variable definition, to avoid an inadvertently deletion of the required empty lines.

(SET LF=^
%=this line is empty=%
)

I have removed to often one of the empty lines and then I searched forever why my program didn't work anymore.

And the paranoid version checks also the newline variable for whitespaces or other garbage.

if "!LF!" NEQ "!LF:~0,1!" echo Error "Linefeed definition is defect, probably multiple invisble whitespaces at the line end in the definition of LF"

FOR /F "delims=" %%n in ("!LF!") do (
  echo Error "Linefeed definition is defect, probably invisble whitespaces at the line end in the definition of LF"
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    That's hot. I've been using this language forever, and I learn completely new things almost weekly. Amazing how many little quirks it has. Thanks – dgo Jan 04 '17 at 18:30
  • I think the OP wanted to store and build a string containing newline characters, and then push the whole string to a text file, which is quite similar to what I am trying to achieve. The thing that you suggest is not working for me... – LPVOID Apr 20 '19 at 12:11
  • 1
    @LPVOID The solution is exactly for the case of building a string, containg newlines. `is not working for me` is not a very helpful description of a problem. – jeb Feb 25 '20 at 07:10
  • `(SET LF=^⏎%=this line is empty=%⏎)` doesn't work in interactive mode but works in `*.bat` files. (That was just a bit of additional details for readers. The answer is amazing.) – Sasha Jul 04 '20 at 19:09
  • 1
    @Sasha You are right, it fails because the `%=this line is empty=%` isn't removed in the command line context. But if you drop the percent part it works again `(SET LF=^⏎⏎) – jeb Oct 07 '20 at 09:04
8

You can define the newline as follows:

set newline=^& echo.

Then, you can use the newline in the echo-statement, like this: (not applicable in the outlined situation, however)

echo %finalcontent%%newline%

or you can use the newline in each set-statement, like this: (mind the extra caret)

set finalcontent=!finalcontent!!currentline!^!newline!

or similarly:

set currentline=%%I %%J %1^%newline%
mousio
  • 10,079
  • 4
  • 34
  • 43
  • 1
    I originally marked this as a -1 because I later found out that this works but not with EnableDelayedExpansion (AKA !var! instead of %var%). Might want to consider adding that to your answer at the top in bold. So I can upvote this, I can't change my vote unless the answer is edited. ;) – Arvo Bowen Jan 14 '15 at 18:02
  • 2
    **not** the -1, but this is ***not*** a real `\n`, but a macro – ScriptKidd Jun 06 '20 at 01:29