windowsbatch-filecmd

Filename timestamp in Windows CMD batch script getting truncated


I have a batch script that outputs a file, and I'm trying to ensure that each time the script is executed, no existing files are overwritten, so I'm trying to put a timestamp on it.

Currently I have this:

set  stamp=%DATE:/=-%_%TIME::=-%

But if the time is 1-9 AM, it gives something like:

13-06-2012_ instead of a full 13-06-2012_12-39-37.28

How can I fix this?

I'm using Windows 7, and the output of echo %date% %time% in a command line window is (my clock format for 'short date' is set to display 3-letter months):

03-Sep-12 9:06:21.54

Basically I want a solution that solves the issue regardless of what the clock format is set to.


Edit: Since no one likes to read past the title, I will explicitly state this question is about a truncation issue. And I found a solution.

I've been using the following timestamp for a good while now, works well.

set timestamp=%DATE:/=-%_%TIME::=-%
set timestamp=%timestamp: =%

It produced a timestamp like: 18-03-2013_13-37-43.26, by replacing / and : in %TIME% and %DATE%, then stripping white space. The whitespace was the problem in my original question, really.


Solution

  • Thanks to an answer to Stack Overflow quesion Creating a file name as a timestamp in a batch job, I found that it was a space terminating the filename.