Windows batch file redirect output to logfile with date/time -


i trying run batch file runs executable , redirects output log file. log file must have date , time file name. command 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. log file created expected '2015_06_25__11_20_46.log'. however, not work when pasted in batch file, , run in cmd. cmd interprets command 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 file name truncated , time appended command arguments instead of being in file name. command fails.

this surely simple have not found way fix this. appreciated.

thank you!

your problem is, timestring may contain space (before 10 o'clock) can replace zero, recommend solution independent of locale settings:

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

it give (independent of locale settings!):

  20130802203023.304000+120  ( yyyymmddhhmmss.<fraction>+/-<timedifference utc>  ) 

from here, easy format needs. example:

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

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -