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

22,697

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:
22,697
oscilatingcretin
Author by

oscilatingcretin

Updated on September 18, 2022

Comments

  • oscilatingcretin
    oscilatingcretin over 1 year

    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?

  • Sean C.
    Sean C. almost 12 years
    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.
  • oscilatingcretin
    oscilatingcretin almost 12 years
    very good! Worked awesomely!
  • will
    will over 8 years
    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?