Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Append text to a text file ON THE SAME LINE using command line

14,377 views
Skip to first unread message

munchk...@gmail.com

unread,
Mar 28, 2012, 10:02:40 PM3/28/12
to
Hi all,

I’m trying to append text to the end of a text file using the command line.

At the moment I have:
Cmd /c ECHO TextHere >> C:\Test.txt

Which, if Text.txt doesn’t exist, creates Test.txt with the following:
“TextHere


Note the new line feed.

If I then execute the line again, the file will look like this:
“TextHere
TextHere


But I need it to be:
“TextHereTextHere”

If I remove the new line feed from the text file before executing the line again, it will achieve this (and append a new line feed at the end).
Given this, do you know either:
1. How to write text to the file without a line feed character at the end, or
2. How to delete the new line feed character from the last row?


Thanks in advanced :)

Nick

munchk...@gmail.com

unread,
Mar 29, 2012, 12:26:40 AM3/29/12
to
Nevermind, I have found the answer after MUCH searching!

Use the following code to write text to a text file withOUT a new line character feed:

cmd /c echo|set /p="TextHere">>C:\Test.txt

Correct me if I'm wrong, but from my understanding, the "set" pipe is used to pass the exact string variable ("TextHere") to the echo function meaning that the new line character is not appended.

Tom Lavedas

unread,
Mar 31, 2012, 1:21:05 PM3/31/12
to
Yes, that's basically true, but this should work and is a bit simpler ...

set /p="TextHere" <nul >>C:\Test.txt

BTW the quotes are only required if you need to send problem characters like the > or < into the file. The explicit invocation oc the command processor is not required and the ECHO pipe is replaced by redirecting the NUL device into the statement.
_____________________
Tom Lavedas

munchk...@gmail.com

unread,
Apr 13, 2012, 11:18:26 PM4/13/12
to
Thanks Tom. You wouldn't believe how many people have asked this question throughout different QA sites and not found an adequate answer..
Cheers

sr.yuva...@gmail.com

unread,
Jun 19, 2012, 8:46:20 AM6/19/12
to
Awesome guys,
exactly am in need of this.
Thanks buddies

mirosla...@gmail.com

unread,
Feb 26, 2016, 1:24:36 AM2/26/16
to
Thanks man, I am needing this right now!
0 new messages