简体   繁体   中英

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% ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM