pzk*_*pfw 10 windows command-line
所以我正在尝试运行foo.exe,但我不希望输出到终端,而是输出到文件中。跑步foo.exe > foo.txt对我来说应该可以做到这一点,但事实并非如此。当我运行 exe 文件时,我得到了输出。换句话说,exe 工作正常。但是,当我尝试将输出发送到文件时,我唯一得到的是:
'c:/Program' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
这仅在我尝试将其发送到文件时显示。考虑到它可能是c:\Program Files (x86)\被误解的路径(等等),我尝试像这样指定输出文件:foo.exe > c:\test.txt,但仍然没有乐趣。
那么,除了说明我尝试运行的二进制文件写得不好之外,我还能做些什么来解决这个问题?请记住,当我只运行 exe 时,我确实得到了有效的输出,它只是不能很好地打印到文件中。显然输出就在那里,问题是是否有某种方法可以捕获它。
Kev*_*gan 22
您尚未显示正在使用的命令失败。如果您在问题中显示它,可能更容易为您找到解决方案。
我希望你的命令是这样的:
C:\>foo.exe|c:\Program Files (x86)\something\test.txt
您收到的错误有点线索:
'c:/Program' is not recognized as an internal or external command, operable program or batch file.
第一的:
... is not recognized as an internal or external command, operable program or batch file.
这通常发生在您尝试使用 a|而不是>.
第二:
'c:/Program' ...
指定包含空格的文件名(或路径)时,必须用双引号 ( "...") 将其括起来。这是因为当操作系统确定要重定向到的文件时,它会在遇到未加引号的空格时停止查找文件名:"c:/Program".
尝试这个:
foo.exe>"c:\Program Files (x86)\something\test.txt"
Run Code Online (Sandbox Code Playgroud)
如果上述方法无法将输出捕获foo.exe到文本文件,则还有另一种可能性......
如果程序foo.exe将其输出写入到STDERR而不是STDOUT,foo.exe则不会通过使用带有单个>. 你必须这样做:
foo.exe>"c:\Program Files (x86)\something\test.txt" 2>&1
Run Code Online (Sandbox Code Playgroud)
这是文件重定向和2>&1符号的解释。
当程序写入终端时,它可以写入两个Streams.
流 1 被称为STDOUT或标准输出。通常,程序将它们的“正常”输出写入流 1。
流 2 被称为STDERR或标准错误。通常,程序将它们的“错误”输出(错误和警告消息)写入流 2。
程序是否将特定输出写入STDOUT或STDERR由程序员以及他们如何编写程序决定。一些程序被编写为将所有输出(正常输出和错误)发送到STDOUT.
当一个程序在没有输出重定向的情况下运行时,所有正常和错误输出都被发送到终端屏幕,STDOUT输出或STDERR输出之间没有任何区别。
当您使用这样的单个进行“正常”重定向时>:
foo.exe > "c:\Program Files (x86)\something\test.txt"
Run Code Online (Sandbox Code Playgroud)
您没有指定将哪个Stream重定向到文件,因此假定为 Stream 1。
就像你这样输入一样:
foo.exe 1> "c:\Program Files (x86)\something\test.txt"
Run Code Online (Sandbox Code Playgroud)
这告诉命令解释器 ( cmd.exe) 将STDOUT(Stream 1)的程序输出捕获到指定的文件名。将1在1>指流1。
在这种情况下,所有正常程序都被捕获到文件中,但如果程序写入STDERR(流 2),则不会捕获该输出并将显示在屏幕上。这通常是执行此操作的“理想”方式,以便在捕获正常程序输出时,您可以在屏幕上看到是否发生错误。
如果要将“正常”输出捕获到一个文件,并将“错误”输出捕获到另一个文件,您可以这样做:
foo.exe > "c:\output.txt" 2> "C:\error.txt"
or
foo.exe 1> "c:\output.txt" 2> "C:\error.txt"
Run Code Online (Sandbox Code Playgroud)
如果您希望将“正常”输出和“错误”输出捕获到同一个文件中,您可以像这样指定它:
foo.exe > "c:\output.txt" 2>&1
Run Code Online (Sandbox Code Playgroud)
这基本上是指定它的“速记”方式,它意味着将流 1 重定向到指定的文件,并将流 2 重定向到与流 1相同的“位置”(文件)。
步行者问道:
foo.exe > "c:\output.txt" 2>&1 和 foo.exe > "c:\output.txt" 2>"c:\output.txt" 有什么区别吗?它们相同吗?
简短回答:您会认为它们是相同的,但事实并非如此。它们是不同的。
With redirection using >"filename.ext", 1>"filename.ext", or 2>"filename.ext", the > causes the output to be written to a new file named "filename.ext". If the file "filename.ext" already exists, it will be deleted first.
So, using:
foo.exe > "c:\output.txt" 2>"c:\output.txt"
causes a "conflict" where both redirections are trying to write to the same file and both are trying to delete the file if it already exists. This will likely cause undesired behavior. Generally, one or the other, or both, of the outputs will NOT be captured fully, or predictably.
The actual result will depend on the operating system and version, and may also depend on the command being executed. What will likely happen is:
1 The output sent to one of the redirections will be captured or partially captured, and the output sent to other redirection will be lost. 2 The operating system will complain about the command and neither of the outputs will be captured (fully). 3 Undefined, undesired, unpredictable, unexpected behavior.
On Windows 7 and likely on Windows Vista/8/10, and possibly on Windows XP, the operating system will complain about command and the command will be canceled.
For example (Windows 7): I have a folder named: "C:\Temp\emptyfolder" and a file named "nonexistantfile" doesn't exist there.
C:\>cd "\Temp\emptyfolder"
C:\Temp\emptyfolder>dir nonexistantfile>output.txt
File Not Found
C:\Temp\emptyfolder>type output.txt
Volume in drive F is FFFFx1tb
Volume Serial Number is 4011-A5C6
Directory of C:\Temp\emptyfolder
C:\Temp\emptyfolder>
Run Code Online (Sandbox Code Playgroud)
In this case, using one redirection (>output.txt), the output of the dir command is captured the the file: output.txt, and the error message File Not Found is shown on the screen... this is the expected behavior.
Now, using both redirections (">file" AND "2>file"):
C:\Temp\emptyfolder>dir nonexistantfile>output.txt 2>output.txt
The process cannot access the file because it is being used by another process.
C:\Temp\emptyfolder>type output.txt
C:\Temp\emptyfolder>
Run Code Online (Sandbox Code Playgroud)
In this case, the operating system complained that the (outout) file is already in use. And the file "output.txt" ends up empty (0 bytes), and the output for both of the redirections was lost.
Now, lastly, using both redirections (">file" AND "2>&1"):
C:\Temp\emptyfolder>dir nonexistantfile>output.txt 2>&1
C:\Temp\emptyfolder>type output.txt
Volume in drive C is CCCCCCCC
Volume Serial Number is 1234-ABCD
Directory of C:\Temp\emptyfolder
File Not Found
C:\Temp\emptyfolder>
Run Code Online (Sandbox Code Playgroud)
In this case, ">file" causes the output for "stream 1" ("standard output") to be captured to the file. And "2>&1" causes the output for "stream 2" ("error output") to be sent through the already redirected "stream 1", and to also be captured to the (same) file.
It is also worth noting that the order is important. Reversing the order like this:
dir nonexistant 2>&1 >output.txt
Run Code Online (Sandbox Code Playgroud)
不一样,可能不会给你想要的结果。
在这种情况下,首先看到并处理的“2>&1”导致“流2”的输出(“错误输出”)被重定向到“流1”当前指向的位置,在那个位置时刻,是(默认情况下)屏幕。并且“>file”导致“stream 1”(“标准输出”)的输出被捕获到文件中。最终结果是命令的输出(“流 1”)将被捕获到文件中,但错误输出(“流 2”)仍将显示在屏幕上(而不是文件中)。
| 归档时间: |
|
| 查看次数: |
63347 次 |
| 最近记录: |