-1

Please read question slowly, its strange, but true.

In windows command prompt, I want echo redirect to print \n as new line to output file, but takes as literal(\n) instead of new line.

echo "A\nB\n\C\n" > temp.txt

If I open the temp.txt, I see output as

A\nB\n\C

rather then,

A
B
C

There is similar questions, like this and this on Stack Overflow. However, none of answer serves my purpose.

I tried, various other options, but didn't work are:

echo  $'A\nB\nC' > temp.txt

echo  $'A\\nB\\nC' > temp.txt

etc.

So, my question, is there to achieve new line I want to achieve in redirect?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Red Boy
  • 5,429
  • 3
  • 28
  • 41

2 Answers2

1
@ECHO OFF
SETLOCAL
SET "var=A bucket of eels\nB\nC"
(
 FOR %%a IN ("%var:\n=" "%") DO ECHO(%%~a
)>u:\x.txt
TYPE u:\x.txt
GOTO :EOF

U: is my test drive

This will show some sensitivity to some symbols.

it works by replacing each \n with " " and then enclosing the resultant string in ". This forms a list of quoted strings so the for executes the echo for each quoted string. The ~ removes the enclosing quotes.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 2
    Good answer but I still feel this is a duplicate question. I would add your answer to the duplicate question I flagged above. Doesn't look like that technique was used as an answer in the question I flagged as the duplicate. – Squashman Jun 08 '18 at 15:23
-1

Here are the response: How can you echo a newline in batch files?

so for your case:

(echo A & echo.B & echo.C) > txt

Or even.. from the same topic:

set n=^&echo.
echo hello %n% world
Cœur
  • 37,241
  • 25
  • 195
  • 267
VANILKA
  • 634
  • 1
  • 13
  • 32
  • 2
    This should be marked as a duplicate answer instead of you posting a link to the answer as an answer. – Squashman Jun 08 '18 at 15:07