-2

I have been trying to write multiple lines of text to a text file in Windows but don't know how to do this. Also, I've searched on the internet a lot about this but all solutions use echo command multiple times.

Is there any way to do this without using echo command multiple times like "cat" in Linux?

phuclv
  • 37,963
  • 15
  • 156
  • 475
hiag
  • 21
  • 1
  • 6
  • 1
    Cat and echo are not even close to the same command. Type and More are more closely related. Regardless of that your question is off topic because you are requesting code. If you can provide code examples you are using and what your input and output looks like we would be more obliged to help you. – Squashman Feb 09 '19 at 20:04
  • 1
    something [like this](https://stackoverflow.com/a/40452235/2152082)? – Stephan Feb 09 '19 at 20:21

2 Answers2

0
more >> filename.txt

Should do what you need

dgo
  • 3,877
  • 5
  • 34
  • 47
  • This thing is good in terminal(cmd) but when executed using .bat file, the text below the line "more >> filename.txt" doesn't add automatically to the text file. – hiag Feb 09 '19 at 19:32
0

To echo text with a single line should be possible although not easy/elegant. You'll need a few more lines to store the new line character before using it

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


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
rem just set once, and after that you can use %%NL%% to echo a new line everywhere you want
echo This is a sentence%NL%that's broke down into 2 lines.%NL%And this is another line.

But why would you want that? That'll make the line super long and nobody likes horizontal scrolling. It's also possible to do that in one line with multiple echo calls

C:\Users>echo This is a very long line of text & echo that requires you to scroll horizontally. & echo And people HATE horizontal scrolls
This is a very long line of text
that requires you to scroll horizontally.
And people HATE horizontal scrolls

So the neatest way is to use powershell

C:\Users>powershell -Command "Write-Host This is`na multiline`ntext"
This is
a multiline
text

`n is the newline because ` is the escape character in powershell

Alternatively just use powershell's echo

C:\Users>powershell -Command "echo 'Each parameter to echo' '(A.K.A Write-Output)' 'will be printed on a separate line' 'like this'"
Each parameter to echo
(A.K.A Write-Output)
will be printed on a separate line
like this

If using multiple lines is allowed, but with just a single echo then escape the new line with ^ and leave the next line blank (i.e. 2 lines of code for each output line)

(echo Line 1^

Line 2^

Line 3) >file.txt
phuclv
  • 37,963
  • 15
  • 156
  • 475