News, Product Information, and Tips
Check out our free utlities in the downloads section

Create a date and time stamp in your batch files

by Steve Wiseman on February 16, 2007 · 10 comments

in Windows

Here is an interesting one. I found a way to take the %date% environment variable, and turn it into a valid string for a filename – without any extra programs or scripts.

For the longest time I used a little utility I created to do this. The problem with that is the utility needs to be around if you want to send the batch file to someone.

What I didn’t know that was that you can use this character combination ‘:~’ to pull a substring out of an environment variable. That is when I realized you could use this to pull out parts of the current date (or time).

Here is how it works. Lets take the %date% variable and print it out

echo %date%

It comes back …At least today ;) .. with

Thu 02/15/2007

Not sure if the length of the day changes. It may be always the same. To be safe we can pull the year, month and day starting from the right.

The :~ substring command works like this:

:~[START POS],[LENGTH]

If [START_POS] is positive or zero the substring will start from the left. If the number [START_POS] is negative it will start from the right. And [LENGTH] is the number of characters in the opposite direction of the starting point.

I know this might be confusing at first, but you will see what I am talking about.

If we wanted to get the current year we could start 4 from the end, and 4 in length. Like this:

echo %date:~-4,4%

For the month we start 7 from the right (Length of Year + Length of Month + 1 Slash)

echo %date:~-7,2%

For the day we start 10 from the right (Length of Year + Length of Month + Length Of Day + 2 Slashes)

echo %date:~-10,2%

Bringing it all together. Lets say I zipped up a folder every night for archival purposes, and wanted a different filename for each day (Not sure if this pkzip syntax is correct, but that is not important for our discussion here)

pkzip c:\ImportantFolder\*.* c:\TempZip.zip
ren C:\TempZip.Zip c:\TempZip_%date:~-4,4%%date:~-7,2%%date:~-10,2%.zip

Which renames our C:\TempZip.Zip to C:\TempZip_20070215.zip

Perfect. I get a date stamped file, and no special vbscript, or command line program is needed.

The same method could be used for the current time

I am still amazed this little trick works.

Like this article? Then sign up for my newsletter to get free tips and software sent right to your inbox once a week. Like you, I hate spam – I will never spam, or sell your email address.

Related Articles:

{ 1 trackback }

Date and time stamp in your batch files
November 11, 2009 at 3:23 pm

{ 9 comments… read them below or add one }

1 John October 14, 2009 at 1:43 pm

Great!!
Working Fine……..

2 date *and* time October 28, 2009 at 9:11 am

here’s the “and time” bit:

I know it might sound obvious, but…
…….for time, use environment variable %time%

3 Steve Wiseman October 28, 2009 at 9:34 am

Good point. Title did not match my article. You can use the same principals with the time variable, and include hours, minutes, seconds, and milliseconds.

Here they are:

Milliseconds: %time:~-2,2%
Seconds: %time:~-5,2%
Minutes: %time:~-8,2%
Hours: %time:~-11,2%

4 Lizzy November 2, 2009 at 1:10 am

Hi,

Thanks, These are very helpful tips.

The problen is that when retrieving the hour from the current time variable,
it appears with a leading space (instead of leading zero), when the hour is less than 10.

Any idea to solve it?

Lizzy

5 Steve Wiseman November 2, 2009 at 8:23 am

Here is the way to trim it:

SET HOUR=%time:~-11,2%
Call :TRIM %HOUR%
GOTO :EOF
:TRIM
Set HOUR=%*
:EOF
REM You would use your trimmed hour right here
@echo %HOUR%

6 Jim November 12, 2009 at 3:51 pm

fyi: the below line should be changed

from this: DATESTMP=%date:~-4,4%%date:~-7,2%%date:~-10,2%%HOUR% SET

swap 10,2 and 8,2 (date to be y/m/d)
insert :TRIM (else blank between hour and rest (m/s_DATA.ZIP)

to this: DATESTMP=%date:~-4,4%%date:~-10,2%%date:~-7,2%%HOUR% :TRIM SET

Great concept. thanks.

7 Steve Wiseman November 12, 2009 at 7:22 pm

What format is your date in DD/MM/YYYY or MM/DD/YYYY…when you echo %date% at the command line?

Thanks.

8 bryan December 28, 2009 at 11:07 pm

Hi Guys,

i am newbie in the Batch file creation and hoping you guys can help and guide me on the following:-

i wanted to created a batch file to :-
1st – zip a folder containing a txt files
2nd – the batch file shall ignore the current date folder
3rd – the batch shall be executed on 1200am daily.
4th – the zipped folder name should retained
5th – once the folder has been zipped, it will be move to a dedicated folder (to another HDD or even to a network folder)

By the way, i am using power archiver 2001.

Thank you in advance..
bryan

9 Israel Torres August 19, 2010 at 10:49 am

@Lizzie – perhaps a little late but here’s how I ended up doing the leading space issue for the time :)

set tmp_time=%time:~-11,2%%time:~-8,2%%time:~-5,2%
:: method 1 use this to trim leading space entirely
::set tmp_time=%tmp_time: =%

:: method 2 use this to replace leading space with 0
set tmp_time=%tmp_time: =0%

@ Steve W. – Thanks!

Israel Torres

Leave a Comment

t