Windows batch file redirect output to logfile with date/time

29,891

Solution 1

Your problem is, that the timestring may contain a space (before 10 o'clock) You can replace it with a zero, but I recommend a solution independent of locale settings:

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I

It will give you (independent of locale settings!):

  20130802203023.304000+120 
( YYYYMMDDhhmmss.<fraction>+/-<timedifference to UTC>  )

from here, it is easy to format it to your needs. For example:

set datetime=%datetime:~0,8%-%datetime:~8,6%
20130802-203023

Solution 2

Here's an Inline version without using SET command

C:\test.bat > C:\log\test_%date:~10%%date:~4,2%%date:~7,2%-%time:~0,2%%time:~3,2%%time:~6,2%.log 2>&1

This example will take the output from C:\test.bat and write a log named test_YYYYMMDD-HHMMSS.log to the C:\log directory

Output file example: c:\log\test_20181008-1121.log

Solution 3

Try like this way :

@echo off
set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

set year=%date:~-4%
echo year=%year%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%
Set MaDate=%day%/%month%/%year%
Set MyTime=%hour%:%min%:%secs%
echo %MaDate%
echo %MyTime%
set DateTimeFile=%year%_%month%_%day%_%hour%_%min%_%secs%.log
echo DateTimeFile=%DateTimeFile%
pause

Or i got another solution from foxidrive :

::From foxidrive
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause

Solution 4

Perhaps the simplest solution for you be this one:

set "dat=%date: =0%"
set "tim=%time: =0%"
"%PROGRAMFILES%\PostgreSQL\9.4\bin\vacuumdb.exe" --username postgres --verbose --analyze --all > E:\Logs\VacuumDB\%dat:~10,4%_%dat:~4,2%_%dat:~7,2%_%tim:~0,2%_%tim:~3,2%_%tim:~6,2%.log 2>&1
Share:
29,891
olivierr91
Author by

olivierr91

Updated on March 19, 2021

Comments

  • olivierr91
    olivierr91 about 3 years

    I am trying to run a batch file which runs an executable and redirects its output to a log file. The log file must have the date and time as the file name. This is the command I am using:

    "%PROGRAMFILES%\PostgreSQL\9.4\bin\vacuumdb.exe" --username postgres --verbose --analyze --all > E:\Logs\VacuumDB\%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%.log 2>&1
    

    This command works when pasted directly in cmd. The log file is created as expected as '2015_06_25__11_20_46.log'. However, it does not work when pasted in a batch file, and then run in cmd. Cmd interprets the command as follow:

    "C:\Program Files\PostgreSQL\9.4\bin\vacuumdb.exe" --username postgres --verbose --analyze --all  8_21_42.log  1>E:\Logs\VacuumDB\2015_06_26_ 2>&1
    

    Notice how the file name is truncated and the time is now appended to the command arguments instead of being in the file name. So obviously the command fails.

    This is surely something very simple but I have not found any way to fix this. Any help is greatly appreciated.

    Thank you!