Windows 7 batch files: How to write string to text file without carriage return AND trailing space?

4

I am trying to have my batch file write a string of text to a text file. At first, the command I was using was writing an extra carriage return to the end of the string, but I found this command that prevented that:

echo|set /p=hello>hello.txt

However, now it's putting a trailing space at the end. I need only the string I specify to be written without any extra characters. Is this possible?

oscilatingcretin

Posted 2012-07-09T15:10:42.317

Reputation: 4 093

Doesn't echo mystring>test.txt do just that? – Sean C. – 2012-07-09T15:14:32.653

That's how I was doing it before when it was adding a carriage return at the end. – oscilatingcretin – 2012-07-09T15:16:27.970

Odd; I just tested it myself. When I open it in vim, the output contains no line breaks or spaces after the string. – Sean C. – 2012-07-09T15:17:31.823

I just tested it in a command window and the carriage return is there when I open it in Notepad. – oscilatingcretin – 2012-07-09T15:18:27.843

I am 100% wrong on this. Sorry. – Sean C. – 2012-07-09T15:37:44.510

Answers

10

This command should work exactly like you want - write only "hello", without any extra symbols.

> echo|set /P ="hello" > foo

> dir foo
...
09.07.2012  19:25                 5 foo
               1 File(s)              5 bytes

Looks like exactly 5 symbols without CR+LF to me.

Oleg V. Volkov

Posted 2012-07-09T15:10:42.317

Reputation: 597

I'm not sure why that should work, however I found it doesn't work for spaces. Anyone got an idea how I can pad on the left? – will – 2016-01-19T01:00:11.863

Oleg is right. The SET /P is designed to output a question and accept input from the user on the same line, so it automatically excludes line breaks. The wc command actually counts LF characters, so it should show 0 if the line feed is actually omitted. In Oleg's example that is true. – Sean C. – 2012-07-09T15:35:09.413

very good! Worked awesomely! – oscilatingcretin – 2012-07-09T15:58:50.087