windows 将时间戳添加到批处理输出的日志行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39715936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add timestamp to log lines from batch output
提问by user3608260
I have a simple batch file that is scheduled to run, and essentially just backs up a remote network share to my computer.
我有一个计划运行的简单批处理文件,基本上只是将远程网络共享备份到我的计算机。
@ECHO OFF
xcopy \10.8.0.1\share\ E:\backup\ /c /s /r /d /y /i >> E:\backup\backup.log
The output in the log looks like this:
日志中的输出如下所示:
\10.8.0.1\share\test.txt
1 File(s) copied
\10.8.0.1\share\New Microsoft Word Document.docx
1 File(s) copied
Basically I just want to add a date and time stamp to each log entry. How do I go about doing this?
基本上我只想为每个日志条目添加一个日期和时间戳。我该怎么做?
回答by soja
You could echo DATE% and %TIME% to backup.log.
您可以将 DATE% 和 %TIME% 回显到 backup.log。
echo %DATE% %TIME% >> E:\backup\backup.log
That's probably the easiest.
这可能是最简单的了。
You could also use robocopy for richer logging options.
您还可以使用 robocopy 获得更丰富的日志记录选项。
回答by geisterfurz007
The standard variables %date%
and %time%
are what you are looking for.
标准的变量%date%
和%time%
你在找什么。
You can pass the lines of a command output as single lines with a for-loop and in between them
您可以将命令输出的行作为带有 for 循环的单行并在它们之间传递
@echo off
for /f "delims=" %%i in ('xcopy \10.8.0.1\share\ E:\backup\ /c /s /r /d /y /i') do (
echo [%date%, %time%] %%i >> E:\backup\backup.log
)
Short explanation: The loop splits the output of the command to the single lines. For each line the program then echos the timestamp and after that one line of the output. Then it repeats for each line getting returned by the xcopy
command.
简短说明:循环将命令的输出拆分为单行。对于每一行,程序然后回显时间戳,然后在输出的那一行之后。然后它对xcopy
命令返回的每一行重复。