记录和排除在Windows上运行的命令或批处理作业的行为的最有用的方法之一是将输出重定向到文件。
但是,有几种不同的方法可以将命令行写入重定向到文件。您选择的选项取决于您希望如何查看命令输出。
Windows 命令提示符输出的工作原理(How Windows Command Prompt Output Works)
当您在Windows控制台(命令提示符(command prompt))中键入命令时,该命令的输出将转到两个单独的流。
- STDOUT:标准输出(Out)是来自命令的任何标准响应。例如,DIR命令的标准响应是目录中的文件列表。
- STDERR:标准错误(Standard Error)是命令出现问题时出现的任何错误消息。例如,如果目录中没有任何文件,DIR命令将输出“ File Not Found”到标准错误(Standard Error)流。
对于这两个输出流,您可以将输出重定向到Windows中的文件。(Windows)
将标准输出写入重定向到新文件(Redirect Standard Output Write to New File)
有两种方法可以将命令的标准输出重定向到文件。第一种是每次运行命令时将命令输出写入发送到一个新文件。
为此,请打开命令提示符并键入:
dir test.exe > myoutput.txt
> 字符告诉控制台将STDOUT 输出(STDOUT)到具有您提供的名称的文件。
运行此命令(run this command)时,您会注意到命令窗口中没有任何响应,除了文件不存在的错误。
这是因为该命令的标准输出被重定向到名为 myoutput.txt 的文件。该文件现在存在于您运行该命令的同一目录中。标准错误输出仍然像往常一样显示。
注意(Note):在运行命令之前,请小心更改命令提示符的活动目录(change the active directory)。这样您就可以知道输出文件的存储位置。
您可以通过在命令窗口中键入“myoutput.txt”来查看文件的标准输出。这将在您的默认文本文件查看器中打开文本文件。对于大多数人来说,这通常是Notepad.exe。
下次您运行相同的命令时,之前的输出文件将被删除。将使用最新命令的输出重新创建一个新的输出文件。
将标准输出(Standard Output)写入重定向到同一文件(Same File)
如果您不想覆盖同一个文件怎么办?另一种选择是使用 >> 而不是 > 来重定向到输出文件。在此示例中,您将键入:
dir test.exe >> myoutput.txt
您将看到相同的输出(仅限错误)。
但在这种情况下,此命令不会覆盖输出文件,而是将新输出附加到现有输出文件中。
每次运行命令并将输出附加到文件时,它都会将新的标准输出写入现有文件的末尾。
将标准错误重定向到文件(Redirect Standard Error To a File)
与您可以将标准输出写入重定向到文件的方式相同,您也可以将标准错误流输出到文件。
为此,您需要将2>添加到命令的末尾,然后是要创建的输出错误文件。
在此示例中,您将键入命令:
dir test.exe > myoutput.txt 2> output.err
这会将标准输出流发送到 myoutput.txt,将标准错误流发送到 output.err。结果是控制台窗口中根本没有显示任何输出流。
但是,您可以通过键入output.err(output.err)查看错误消息。这将在您的默认文本文件查看器中打开该文件。
如您所见,命令中的任何错误消息都会输出到错误文件中。与标准输出一样(Just),您可以使用>>将错误附加到先前运行的命令的错误中。
将所有输出写入重定向到同一文件(Redirect All Output Writes to a Same File)
上述所有方法都会产生多个文件。一个文件用于标准输出流,另一个用于标准错误流。
如果您想将这两个输出包含到同一个文件中,您也可以这样做。为此,您只需使用以下命令将所有输出重定向到同一文件。
dir test.exe 1> myoutput.txt 2>&1
以下是此命令的工作原理:
- 标准输出被定向到由输出编号 1 标识的输出文件。
- 由数字 2 标识的标准错误输出被重定向到由数字 1 标识的输出文件。
这会将错误输出附加到标准输出的末尾。
这是查看一个文件中任何命令的所有输出的有用方法。
使标准或错误输出流静音(Silencing Standard or Error Output Streams)
您还可以通过将输出重定向到NUL而不是文件来关闭标准输出(Standard Output)或标准错误。(Standard Error)
使用上面的示例,如果您只需要标准输出(Standard Output)而根本不需要标准错误(Standard Error),则可以使用以下命令:
dir test.exe 1> myoutput.txt 2>nul
这将产生与上面第一个示例相同的输出文件,其中您仅重定向了Standard Output,但使用此命令,错误不会在控制台内回显。它也不会创建错误日志文件。
如果您不关心任何错误并且不希望它们成为麻烦,这将很有用。
您可以从BAT 文件(inside a BAT file)中执行上述任何相同的输出命令,该行的输出将转到您指定的输出文件。这是查看BAT文件中的任何命令在尝试运行时是否有任何错误的有用方法。
Redirect Output from the Windows Command Line to a Text File
One of thе moѕt useful ways to log and troublеshoot the behаvior of commands or bаtch jobs that you run on Windоws is to redirect output to a file.
However, there are a few different ways you can redirect command line writes to a file. The option you choose depends on how you want to view your command output.
How Windows Command Prompt Output Works
When you type a command in the Windows console (command prompt), the output from that command goes to two separate streams.
- STDOUT: Standard Out is where any standard responses from commands go. For example the standard response for the DIR command is a list of files inside a directory.
- STDERR: Standard Error is where any error messages go if there’s a problem with the command. For example if there aren’t any files in the directory, the DIR command will output “File Not Found” to the Standard Error stream.
You can redirect output to a file in Windows for both of these output streams.
Redirect Standard Output Write to New File
There are two ways you can redirect standard output of a command to a file. The first is to send the command output write to a new file every time you run the command.
To do this, open the command prompt and type:
dir test.exe > myoutput.txt
The > character tells the console to output STDOUT to the file with the name you’ve provided.
When you run this command, you’ll notice that there isn’t any response in the command window except the error that the file doesn’t exist.
This is because the standard output for the command was redirected to a file called myoutput.txt. The file now exists in the same directory where you ran the command. The standard error output still displays as it normally does.
Note: Be careful to change the active directory for the command prompt before running the command. This way you’ll know where the output files are stored.
You can view the standard output that went to the file by typing “myoutput.txt” in the command window. This will open the text file in your default text file viewer. For most people, this is usually Notepad.exe.
The next time you run the same command, the previous output file will be deleted. A new output file will be recreated with the latest command’s output.
Redirect Standard Output Writes to the Same File
What if you don’t want to overwrite the same file? Another option is to use >> rather than > to redirect to an output file. In the case of this example, you would type:
dir test.exe >> myoutput.txt
You’ll see the same output (the error only).
But in this case, instead of overwriting the output file, this command appends the new output to the existing output file.
Every time you run a command and append the output to a file, it’ll write the new standard output to the end of the existing file.
Redirect Standard Error To a File
The same way you can redirect standard output writes to a file, you can also output the standard error stream to a file.
To do this, you’ll need to add 2> to the end of the command, followed by the output error file you want to create.
In this example, you’ll type the command:
dir test.exe > myoutput.txt 2> output.err
This sends the standard output stream to myoutput.txt, and the standard error stream to output.err. The result is that no output stream at all gets displayed in the console window.
However, you can see the error messages by typing output.err. This will open the file in your default text file viewer.
As you can see, any error messages from the command are output to the error file. Just as with the standard output, you can use >> instead to append the error to errors from previously run commands.
Redirect All Output Writes to a Same File
All of the approaches above result in multiple files. One file is for the standard output stream and the other is for the standard error stream.
If you want to include both of these outputs to the same file, you can do that too. To do this, you just need to redirect all output to the same file using the following command.
dir test.exe 1> myoutput.txt 2>&1
Here’s how this command works:
- The standard output is directed to the output file identified by output number 1.
- The standard error output identified by the number 2 is redirected to the output file identified by number 1.
This will append the error output to the end of the standard output.
This is a useful way to see all output for any command in one file.
Silencing Standard or Error Output Streams
You can also turn off either Standard Output or Standard Error by redirecting the output to a NUL instead of a file.
Using the example above, if you only want Standard Output and no Standard Error at all, you can use the following command:
dir test.exe 1> myoutput.txt 2>nul
This will result in the same output file as the first example above where you only redirected the Standard Output, but with this command the error won’t echo inside the console. It won’t create an error log file either.
This is useful if you don’t care about any errors and don’t want them to become a nuisance.
You can perform any of the same output commands above from inside a BAT file and the output from that line will go to the output file you specify. This is a useful way to see whether any commands within a BAT file had any errors when they tried to run.