Reputation: 14957
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.
Upvotes: 49
Views: 254908
Reputation: 41234
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 . . .
Upvotes: 83
Reputation: 10595
See Stack Overflow question How to get current datetime on Windows command line, in a suitable format for using in a filename?.
Create a file, date.bat
:
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-3 delims=/:/ " %%a in ('time /t') do (set mytime=%%a-%%b-%%c)
set mytime=%mytime: =%
echo %mydate%_%mytime%
Run date.bat
:
C:\>date.bat
2012-06-14_12-47-PM
UPDATE:
You can also do it with one line like this:
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g
Upvotes: 13
Reputation: 1
BATCH/CMD FILE like DateAndTime.cmd (not in CMD-Console)
Code:
SETLOCAL EnableDelayedExpansion
(set d=%date:~8,2%-%date:~3,2%-%date:~0,2%) & (set t=%time::=.%) & (set t=!t: =0!) & (set STAMP=!d!__!t!)
Create output:
echo %stamp%
Output:
2020-02-25__08.43.38.90
Or also possible in for lines in CMD-Console and BATCH/CMD File
set d=%date:~6,4%-%date:~3,2%-%date:~0,2%
set t=%time::=.%
set t=%t: =0%
set stamp=%d%__%t%
"Create output" and "Output" same as above
Upvotes: 0
Reputation: 1008
Windows batch log file name in 2018-02-08_14.32.06.34.log format:
setlocal
set d=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
set t=%time::=.%
set t=%t: =%
set logfile="%d%_%t%.log"
ping localhost -n 11>%1/%logfile%
endlocal
Upvotes: 2
Reputation: 216
Here's a batch script I made to return a timestamp. An optional first argument may be provided to be used as a field delimiter. For example:
c:\sys\tmp>timestamp.bat
20160404_144741
c:\sys\tmp>timestamp.bat -
2016-04-04_14-45-25
c:\sys\tmp>timestamp.bat :
2016:04:04_14:45:29
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: put your desired field delimiter here.
:: for example, setting DELIMITER to a hyphen will separate fields like so:
:: yyyy-MM-dd_hh-mm-ss
::
:: setting DELIMITER to nothing will output like so:
:: yyyyMMdd_hhmmss
::
SET DELIMITER=%1
SET DATESTRING=%date:~-4,4%%DELIMITER%%date:~-7,2%%DELIMITER%%date:~-10,2%
SET TIMESTRING=%TIME%
::TRIM OFF the LAST 3 characters of TIMESTRING, which is the decimal point and hundredths of a second
set TIMESTRING=%TIMESTRING:~0,-3%
:: Replace colons from TIMESTRING with DELIMITER
SET TIMESTRING=%TIMESTRING::=!DELIMITER!%
:: if there is a preceeding space substitute with a zero
echo %DATESTRING%_%TIMESTRING: =0%
Upvotes: 7
Reputation: 283
In the past, I've used a .cmd script I found on the Internet. I hate the way localization normally messes with dates. Anytime you have dates in filenames (or anywhere else, if I may be so bold) I figure you want them in ISO 8601 format:
2015-02-19T14:54:51Z
or something else that has Y M D H M in that order, such as
2015-02-19 14:54
because it fixes the MDY / DMY ambiguity and because it's sortable as text.
I don't know where I got that .cmd script, but it may have been http://ss64.com/nt/syntax-getdate.html, which works beautifully on my YYYY-MM-DD Windows 8.1 and on a M/D/YYYY vanilla install of Windows 7. Both give the same format:
2015-02-09 04:43
Upvotes: 0
Reputation: 14957
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.
Upvotes: 2
Reputation: 4055
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time: =0%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g
echo %DateNtime%
Or, from the command line:
for /f "tokens=2-8 delims=.:/ " %a in ("%date% %time: =0%") do echo %c-%a-%b_%d-%e-%f.%g
EDIT: As per bryce's non-standard time/date specs. (03-Sep-12 9:06:21.54
)
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-7 delims=.:/- " %%a in ("%date% %time%") do (
if "%%b"=="Jan" set MM=01
if "%%b"=="Feb" set MM=02
if "%%b"=="Mar" set MM=03
if "%%b"=="Apr" set MM=04
if "%%b"=="May" set MM=05
if "%%b"=="Jun" set MM=06
if "%%b"=="Jul" set MM=07
if "%%b"=="Aug" set MM=08
if "%%b"=="Sep" set MM=09
if "%%b"=="Oct" set MM=10
if "%%b"=="Nov" set MM=11
if "%%b"=="Dec" set MM=12
set HH=0%%d
set HH=!HH:~-2!
echo 20%%c-!MM!-%%a_!HH!-%%e-%%f.%%g
)
endlocal
Upvotes: 4
Reputation: 9430
It wants the full time in DD-MM-YYYY_HH-MM-SS.TT
where TT is the ticks. The exception says it all.
Upvotes: 0