zuku
zuku

Reputation: 11

Append date, time to batch log file

I have batch script that stop, start windows services:

net stop Tomcat6 1>>C:\logs\"%date%"\log.txt 2>&1

but in this log I have only information, that "service is stopping" and "service has been stopped". How could modify this script to append in front of every output message to have %date%-%time% ?

Upvotes: 1

Views: 2547

Answers (2)

AR Khan
AR Khan

Reputation: 1

I Hope This Will Help For you.

@echo off
echo.
echo %date% %time% && net stop service_Name >>%systemdrive%\Log\Logs.txt 2>&1
::ping www.google.com >nul
::For Slow Run,Remove Comments (Remove This line also)
Exit

Upvotes: 0

Stephan
Stephan

Reputation: 56155

Use a for/f loop to capture each line of your command (you have to redirect STDERR here, or it would be lost). Then just echo date, time and the captured line.

@echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%a in ('"net stop Tomcat6 2>&1"') do (
  echo !date!-!time! %%a
))>"C:\logs\%date%\log.txt"

Note: depending on your locale settings, you may have to adapt the date string in "C:\logs\%date%\log.txt" to make it a valid folder\filename.

Upvotes: 3

Related Questions