Tuesday, December 22, 2009

Embedding date and time into a file in windows - "But it worked last night"

I'm patching a tiny project at work with duct tape until we can get an enterprise solution. A small batch job runs on the windows scheduler doing something like this:

MyCommand.exe  --output=Saturday1201amUS_%date:~10,4%-%date:~4,2%-%date:~7,2%-%time:~0,2%-%time:~3,2%-%time:~6,5%.txt


I ran it yesterday afternoon and it worked great. It created a file named
Saturday1201amUS_2009-12-21-15-46-00.21.log. This morning I did another test with my client and it failed miserably after creating a file named 'Saturday1201amUS_2009-12-22-'. A little investigation turned up that windows does not prepend a zero in front of the hours before noon so my executable file was getting a space in front of 9 oclock:

MyCommand.exe  --output=Saturday1201amUS_Saturday1201amUS_2009-12-22- 9-47-20.31.log

It worked great after putting quotes around the file name.
MyCommand.exe  --output="Saturday1201amUS_%date:~10,4%-%date:~4,2%-%date:~7,2%-%time:~0,2%-%time:~3,2%-%time:~6,5%.txt"

No comments: