I noticed that when I use echo to print something to a file in DOS, a space is appended to the string. I need to print the string without the trailing space. Is there a way to do that, or as a workaround, remove trailing spaces from the file?
If I understood the problem correctly, you wrote the trailing space.
Instead of
echo string > file
use
echo string>file
Assuming you're talking about cmd.exe
rather than the actual (rather outdated) MSDOS, there are a number of ways to do this, the first being:
echo Here is some text>output.txt
but I find that somewhat less than readable since I'm used to being able to clearly delineate 'arguments' on the command line.
Alternatively, there's nothing stopping you from swapping around the order of your command line:
>output.txt echo Here is some text
which will allow you to still separate the arguments whilst not having extraneous spaces put in your output file.
In fact, I've often used this method for blocks of code as well:
>output.txt (
echo hello
echo goodbye
)
which will write both lines to the file. I find it preferable in that case since you know right at the start where the output is going, rather than having to go and look at the end of the code block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With