Using echo without trailing space in DOS

只谈情不闲聊 提交于 2019-12-22 05:21:59

问题


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?


回答1:


If I understood the problem correctly, you wrote the trailing space.

Instead of

echo string > file

use

echo string>file



回答2:


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.




回答3:


Some quick searching brought me this link

It seems you have multiple options. If you want to parse the file post-echo using a script, you could consider this VBScript

  Do While Not WScript.StdIn.AtEndOfStream
    WScript.Echo RTrim(WScript.StdIn.ReadLine)
  Loop

which loops, line by line through a file(usually .txt), and performs RTrim, which strips trailing spaces.

Cheers



来源:https://stackoverflow.com/questions/12505552/using-echo-without-trailing-space-in-dos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!