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!