4

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!

olivierr91
  • 1,243
  • 3
  • 13
  • 29
  • 1
    try with `> "E:\Logs\VacuumDB\%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%.log" 2>&1` – npocmaka Jun 26 '15 at 12:42

4 Answers4

7

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

Stevie G
  • 5,638
  • 1
  • 10
  • 16
6

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
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
2

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
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1

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
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks it works, but since the date/time format is not the same across all local settings, the mid string function does not always extract the right characters. Stephan's solution solves this problem. – olivierr91 Jun 27 '15 at 14:49
  • Well, you didn't indicated that you had local problems. After your example code, you said: "The log file is created as expected as '2015_06_25__11_20_46.log'"... **`:/`** – Aacini Jun 27 '15 at 19:07
  • Yes I know, but I tried your example and it outputs the wrong format on my end (because my locale is fr-CA), and that highlights the locale problem. Since I can only choose one anwser I chose the one who fixed the locale problem... Sorry about that, but I voted you up anyway... – olivierr91 Jun 29 '15 at 01:29
  • I think there is a confusion here. I copy-pasted your code and just added the replacement of spaces by zeros, so all substring positions are _exactly the same_ you posted before "The log file is created as expected". However, in your previous comment you said that _the same substrings_ "outputs the wrong format on my end (because my locale is fr-CA)"??? I don't care rep points, but clear this detail is important to me because English is not my primary language. I would like that you note if I misunderstood some point, please; otherwise I just expect you say: "My mistake, excuse me..." **`:/`** – Aacini Jun 29 '15 at 03:22
  • I pasted your solution on another computer which has a different locale than the one I used to raise my original problem, that's what happened and why it output a wrong format. The other member actually foresaw a problem I did not actually raise, and thought about something that could eventually turn out to be a problem (and I found out myself that it is). So yes your solution did solve the problem I originally posted, there is no doubt about that. No one can be blamed on this. Thank you for your answer and taking time to try and help me... – olivierr91 Jun 30 '15 at 03:51
  • _"I tested your solution on another computer which has a different behavior than the one that raised the original problem"_. This point is against the most basic rules of computer-support forums, but the worst part is that you gave not a single advice about previous point when you indicated that my solution "outputs the wrong format". Try to understand my point of view: if I ignore this situation then the only possible conclusion is that your computer or the mine fails in an unpredictable way!!! If you insist that "No one can be blamed on this", then any further communication is useless... – Aacini Jul 01 '15 at 04:12