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.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher, using WMIC.
@echo off for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & 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 "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%" echo datestamp: "%datestamp%" echo timestamp: "%timestamp%" echo fullstamp: "%fullstamp%" pause
Output example:
datestamp: "20200828" timestamp: "085513" fullstamp: "2020-08-28_08-55-13" Press any key to continue . . .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With