[英]CMD Echo Command Keeps Only Last Line of Text
I'm creating a batch file and I'm having trouble with the following command: 我正在创建一个批处理文件,我遇到以下命令时遇到问题:
echo Hello & echo.World > Text.txt
Whenever I do this command the output only shows the last line, like so: 每当我执行此命令时,输出仅显示最后一行,如下所示:
World 世界
But if I just do the above command in CMD without writing to a file, it appears perfectly in the command prompt. 但是,如果我只是在CMD中执行上述命令而不写入文件,它在命令提示符中就会完美显示。 I have complete access over my file but I still can't seem to be able to write the whole text. 我可以完全访问我的文件,但我似乎仍然无法编写整个文本。
In batch, the >
operator has higher precedence than the &
operator, so your example is only redirecting the last command echo.World
to the file. 在批处理中, >
运算符的优先级高于&
运算符,因此您的示例仅将最后一个命令echo.World
重定向到该文件。 The first command is still outputting to the console. 第一个命令仍在输出到控制台。
To redirect multiple commands, you can surround them with parentheses. 要重定向多个命令,可以用括号括起它们。
(echo Hello & echo.World) > Text.txt
In batch & is a command-separator. 批处理&是一个命令分隔符。 You can produce the same result by using two separate echo
lines. 您可以使用两个单独的echo
线生成相同的结果。
echo Hello & echo.World > Text.txt echo Hello&echo.World> Text.txt
executes as two independent commands, 作为两个独立的命令执行,
echo Hello echo.World > Text.txt echo Hello echo.World> Text.txt
which would report Hello
to the console and write World
to the file (you didn't mention that Hello
appeared on the console...) 它会向控制台报告Hello
并将World
写入文件(你没有提到Hello
出现在控制台上......)
Note that > creates a new file whereas >> appends to any existing file (or creates a new file) so 请注意>>创建一个新文件,而>> 附加到任何现有文件(或创建一个新文件)
echo Hello > Text.txt
echo.World > Text.txt
would first write Hello
to a new file, then overwrite it with World
. 首先将Hello
写入新文件,然后用World
覆盖它。
To append the second line, you'd need 要附加第二行,您需要
echo Hello > Text.txt
echo.World >> Text.txt
or, on one line, 或者,在一条线上,
echo Hello > Text.txt&echo.World >> Text.txt
A parenthesised statement-sequence (aka a "block") is regarded as one complex statement, so 带括号的语句序列(又称“块”)被视为一个复杂语句,因此
(echo Hello &echo.World)> Text.txt
would create a new file containing both lines, whereas 会创建一个包含两行的新文件,而
(echo Hello &echo.World)>> Text.txt
would append the two lines to any existing file. 将两行附加到任何现有文件。
Note also that the trailing spaces before the > will also be written to the file. 另请注意, >之前的尾随空格也将写入文件。
Be very careful about > and >> . 关于>和>>要非常小心。 if the command you execute is 如果你执行的命令是
echo Hello 1> Text.txt
then hello Space will be written to the file. 然后将hello Space写入文件。
If a message terminating with Space a single digit is written to a file, then well, probably nothing will appear. 如果以空格终止的消息将一个数字写入文件,那么很可能不会出现任何内容。 When we have this situation, the digit immediately before the > determins which 'logical device output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
当我们遇到这种情况时,紧接在>确定output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
“逻辑设备output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
”之前的数字output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
2>errorreports.txt` for instance. output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
2> errorreports.txt` output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
消息。
If you have this situation, you can overcome this characteristic by using 如果您遇到这种情况,可以通过使用来克服这一特性
> Text.txt echo Hello 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.