14

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?

user3608260
  • 143
  • 1
  • 1
  • 6
  • 1
    What about replacing `@echo off` by `@echo on` and `@prompt $D, $T:$S` and appending `@prompt` to you batch file and redirect it as a whole, like `batch-file.bat > log-file.txt`? – aschipfl Sep 27 '16 at 08:11

2 Answers2

20

You could echo DATE% and %TIME% to backup.log.

echo %DATE% %TIME% >> E:\backup\backup.log

That's probably the easiest.

You could also use robocopy for richer logging options.

soja
  • 1,587
  • 8
  • 8
13

The standard variables %date%and %time% are what you are looking for.

You can pass the lines of a command output as single lines with a for-loop and in between them

@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.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • 3
    Any ideas on how to make it async, so the lines are logged as soon as the parent process outputs them and don't need to wait before it is fully completed? – Dmitri Tsoy Jun 10 '20 at 06:46