Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Doesn't echo mystring>test.txt do just that? – Sean C. Jul 9 '12 at 15:14
    
That's how I was doing it before when it was adding a carriage return at the end. – oscilatingcretin Jul 9 '12 at 15:16
    
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. Jul 9 '12 at 15:17
    
I just tested it in a command window and the carriage return is there when I open it in Notepad. – oscilatingcretin Jul 9 '12 at 15:18
    
I am 100% wrong on this. Sorry. – Sean C. Jul 9 '12 at 15:37
up vote 6 down vote accepted

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.

share|improve this answer
    
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. Jul 9 '12 at 15:35
    
very good! Worked awesomely! – oscilatingcretin Jul 9 '12 at 15:58
    
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 Jan 19 at 1:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .