生成Windows提示符窗口在运行批处理文件时的日志文件方法:

huangapple go评论65阅读模式
英文:

How to generate a log file of the windows prompt when I run a bat file

问题

我正在Windows上运行一个批处理文件。我试图生成一个命令提示符中出现的所有输出的日志文件,以供作为文档使用。

注意,不是批处理文件的内容的日志文件,而是它输出的命令提示符的内容。

我应该如何做到这一点?谢谢。

英文:

I'm running a bat file in windows. I'm trying to generate a log file of all the output that appears in the command prompt, to have as a document.

Note, Not a log file of the contents of the bat file but of the command prompt that it outputs.

How would I do this? Thanks

答案1

得分: 3

重定向输出可以使用 > 或追加到文件使用 >>

对于 batch-file,我们通常这样调用它们。

(call script.cmd)2>&1>"logfile.log"

或追加

(call script.cmd)2>&1>>"logfile.log"

注意,2>&1stderr2 重定向到 stdout1,这在这里很重要,因为你说你要记录所有的输出结果到日志文件。

这也说明你可以将成功 (stdout) 的结果重定向到一个文件,将失败 (stderr) 的结果重定向到另一个文件,例如

(call script.cmd) 1>"Output.log" 2>"Errors.log"

注意,一些命令和可执行文件将所有内容发送到 stdout 流,而不发送到 stderr,例如 ping.exe

英文:

Redirecting to output is done by using > or appending to file using >>

for batch-file, we typically call them.

(call script.cmd)2>&1>"logfile.log"

or append

(call script.cmd)2>&1>>"logfile.log"

Note, 2>&1 2>&1 is redirecting the stderr stream 2 to the stdout stream 1, it is important here, seeing as you said you want to log all of the output results to logfile.

So that should also give the clue that you can in fact redirect success (stdout) results to one file and failures (stderr) to another, i.e

(call script.cmd) 1>"Output.log" 2>"Errors.log"

Note, some commands and executables sends everything to the stdout stream and nothing to stderr, example ping.exe.

答案2

得分: 2

你可以在批处理文件的开头分配一个标签,然后使用"call"命令并将输出重定向到一个文件。以下是我一个批处理文件的示例:

@echo off
call :script > "C:\path\logfile.txt" 2>&1
exit

:script
rem 在这里编写你的脚本

假设你有一个简单的批处理文件,它会要求用户输入一个单词,然后重复显示该单词,如下所示:

@echo off
set /p input="你想要打印什么单词? "
echo %input%

要使此脚本将输出写入文件而不是控制台窗口,你可以这样做:

@echo off
call :script > "C:\path\logfile.txt" 2>&1
exit /b

:script
set /p input="你想要打印什么单词? "
echo %input%

现在,输出将写入到C:\path\logfile.txt文件而不是控制台窗口。

英文:

You can assign a label at the start of the batch file, then use call and redirect the output to a file.
Here is an example from one of my batch files:

@echo off
call :script > "C:\path\logfile.txt" 2>&1
exit

:script
rem your script goes here

Let's say you have a simple batch file that asks the user for a word then repeats that word such as this:

@echo off
set /p input="What word do you want to print? "
echo %input%

To make this script output to a file instead of the console window, you would do this:

@echo off
call :script > "C:\path\logfile.txt" 2>&1
exit /b

:script
set /p input="What word do you want to print? "
echo %input%

This will now output to C:\path\logfile.txt instead of the console window.

huangapple
  • 本文由 发表于 2023年6月1日 10:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定