Update: Now that it's 2016 I'd use PowerShell for this unless there's a really compelling backwards-compatible reason for it, particularly because of the regional settings issue with using date
. See @npocmaka's https://stackoverflow.com/a/19799236/8479
What's a Windows command line statement(s) I can use to get the current datetime in a format that I can put into a filename?
I want to have a .bat file that zips up a directory into an archive with the current date and time as part of the name, for example, Code_2008-10-14_2257.zip
. Is there any easy way I can do this, independent of the regional settings of the machine?
I don't really mind about the date format, ideally it'd be yyyy-mm-dd, but anything simple is fine.
So far I've got this, which on my machine gives me Tue_10_14_2008_230050_91
:
rem Get the datetime in a format that can go in a filename.
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%
rem Now use the timestamp by in a new ZIP file name.
"d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code
I can live with this, but it seems a bit clunky. Ideally it'd be briefer and have the format mentioned earlier.
I'm using Windows Server 2003 and Windows XP Professional. I don't want to install additional utilities to achieve this (although I realise there are some that will do nice date formatting).
I had a similar problem. I have an automatic daily download from an FTP server of an encrypted file. I wanted to decrypt the file using gpg, rename the file to the current date (YYYYMMDD format) and drop the decrypted file into a folder for the correct department.
I went through several suggestions for renaming the file according to date and was having no luck until I stumbled upon this simple solution.
It worked perfectly (i.e., the filename comes out as "2011-06-14.txt").
(Source)
Regionally independent date time parsing
The output format of
%DATE%
and of thedir
command is regionally dependent and thus neither robust nor smart. date.exe (part of UnxUtils) delivers any date and time information in any thinkable format. You may also extract the date/time information from any file withdate.exe
.Examples: (in a cmd-script use %% instead of %)
date.exe +"%Y-%m-%d"
2009-12-22
date.exe +"%T"
18:55:03
date.exe +"%Y%m%d %H%M%S: Any text"
20091222 185503: Any text
date.exe +"Text: %y/%m/%d-any text-%H.%M"
Text: 09/12/22-any text-18.55
Command: date.exe +"%m-%d """%H %M %S """"
07-22 "18:55:03"`
The date/time information from a reference file:
date.exe -r c:\file.txt +"The timestamp of file.txt is: %Y-%m-%d %H:%M:%S"
Using it in a CMD script to get year, month, day, time information:
Using it in a CMD script to get a timestamp in any required format:
Extracting the date/time information from any reference file.
Adding to a file its date/time information:
date.exe is part of the free GNU tools which need no installation.
NOTE: Copying
date.exe
into any directory which is in the search path may cause other scripts to fail that use the Windows built-indate
command.A function that is based on
wmic
:Upside: Region independent. Downside: Only system administrators can run wmic.exe.
Usage:
This echos a string like this:
Note that function parameters are out-Parameters - that is, you must supply variable names instead of values.
All parameters are optional, so
call:Now Y M
is a valid call if you only want to get year and month.See Windows Batch File (.bat) to get current date in MMDDYYYY format:
If you prefer the time in 24 hour/military format, you can replace the second FOR line with this:
If you want the date independently of the region day/month order, you can use "WMIC os GET LocalDateTime" as a source, since it's in ISO order:
I note that the o/p did not ask for a region-independent solution. My solution is for the UK though.
This is the simplest possible solution, a 1-line solution, for use in a Batch file:
Please use the following script to get the current day in the command line: