Skip to content.

TalkBMC

Sections
You are here: Home » Blogs » Anne Gentle » Exploring Information Design and Development » MS-DOS tips for time and date stamps

MS-DOS tips for time and date stamps MS-DOS tips for time and date stamps

Document Actions
How to make zip files and folders with a formatted date and time stamp as the folder name in a DOS batch file automatically

We have a help rendition system internally, where all we writers do is create the source files needed to make CHM files (HHK, HHC, HHP, HTML), zip up the HTML source in a certain way with a certain file name, and submit it using a webform. Out of that rendition engine we can get CHM, JavaHelp,WinHelp, NetHelp, or our internally-created cross-platform help called Browser Help. Browser Help is the system that works with BMC Performance Manager and I've posted before about how to create those help systems automatically.

Since I needed to render several help systems at once, meaning I had to make multiple zip files containing the right content with the right zip file name, and I dislike tedious repetitive tasks, but somehow I like the tedium of testing and using scripts, I used DOS batch files to do the file creation and name formatting for me.

Today's tip is how to make folders with a date and time stamp as the folder name. Our rendition engine likes its zip files with a certain naming convention. As an added bonus, this routine works before and after ten in the morning. Silly DOS, it doesn't put a leading zero in unless you tell it to. Here's how to tell DOS to put in the leading zero if the current time is prior to 10:00am.

REM Create sub directory called \yymmdd_hhmmss
REM where yymmdd_hhmmss is a date_time stamp like 030902_134200
set hh=%time:~0,2%
REM Since there is no leading zero for times before 10 am, have to put in
REM a zero when this is run before 10 am.

if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
md h:\%yymmdd_hhmmss%

A Command-line reference A-Z that contains all the reference information for DOS command-line parameters is available on the Microsoft site.


_____
tags:
Thursday, May 25, 2006 in technology  |  Permalink |  Comments (59)

But I need yyyymmdd_hhmmss!!!!

Posted by Bob at 2006-05-30 12:41
Great script Anne! Works like a charmer.

But, I needed a yyyy version!!!

So, I did ended up with the following.

REM'd the yymmdd SET and replaced it with a SET YYYYMMDD and then diddled with the offset's in the parsing routine.

REM set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
SET YYYYMMDD_HHMMSS=%DATE:~6,4%%DATE:~0,2%%DATE:~3,2%_%HH%%TIME:~3,2%%TIME:~6,2%

Thanks for this handy DOS script to set the environment variabel, and allowing me to create a file name with a timestamp!!

MS-DOS Tips for Time and Date Stamps

Posted by Joe Randle at 2006-06-05 11:18
I found you via an Internet search and thought I had found the answer to my problem, however, your answer is not quite working for me. I have a MS-DOS batch file which renames a *.txt file to the current date and time stamp string. This really works fine, however, it does not work before 10:00 AM. I have set the Regional Settings thinking this would correct it, but it does not.

Here is the line which renames the file:

ren dtfile1.txt DATA-%date:~-4,4%%date:~-10,2%%date:~-7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%.txt

As long as we do the action after 10:00 AM, it works fine, otherwise, it does not work because it finds a space where a digit should be. Attempts to make the machine always see "military time" don't work.

-

Posted by Bala at 2006-06-07 09:59
Thanks for the script, Timely help to me

MS-DOS

Posted by ram at 2006-11-01 16:09
This code is very useful to me; I have been searching for this.

I need one more help from you, I pass password as a parameter to my batch file, when password contains “=”, “%”, “&” batch file throwing password not correct error, how can tell to ignore these characters?

yeah

Posted by dubidu at 2006-12-04 02:47
thanks a lot man, this helped me for my job ;)

Date Script

Posted by Hugo at 2007-05-03 14:20
Thanks a lot!!

help

Posted by MOFO at 2007-05-08 08:30
How can i edit this code to make it time-stamp a folder in this format: YYYY-MM-DD_HH.MM.SS
Ex. 2007-05-08_05.30.12

NEED SUM HELP..!!!

Posted by MOFO at 2007-05-11 21:04
Messed around with numbers a bit and this seems to work for me..
set hh=%time:~0,2%
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set yyyy-mm-dd__hh.mm.ss=%date:~10,4%-%date:~4,2%-%date:~7,2%__%hh%.%time:~3,2%.%time:~6,2%---%date:~-0,3%
MD c:\%yyyy-mm-dd__hh.mm.ss%

Gives me Folder Names like this: 2007-05-09__22.17.28---Wed

So my next question would be:
Is it possible to change the time from 24H time to 12H time and Have it insert AM or PM into the time-stamp so it would look like this:
2007-05-09__12.17.28.AM---Wed
2007-05-09__12.17.28.PM---Wed

2007-05-09__01.17.28.AM---Wed
2007-05-09__01.17.28.PM---Wed

File creation date

Posted by Alex Montalvo at 2007-05-30 17:40
I am looking to make a dos .bat append a file creation date to the end of multiple pdf files cannot use the system date tried various methods with the rename command but I only get today's date.

Great info

Posted by Scott at 2007-06-12 15:29
All the other DOS BATCH date/time solutions I found with my google search were convoluted scripts that generated temporary batch files and were very difficult to follow. Your solution is very straigh-forward and elegant. If only there were some way to get your entry to show up at the top of google's list...

Internationalisation

Posted by PMC at 2007-07-12 10:50
Given that different national settings will produced different results in %date%, is there any way to produce a global-save version of this code? I can't see any way to determine current locale in DOS.
e.g.
US will have date MM/DD/YYYY
UK will have date DD/MM/YYYY

Thanks

Posted by Syed at 2007-08-11 02:23
Thanks this script is what i want

Thank you

Posted by sesan at 2007-09-28 10:49
Worked Great!

Thank you

Posted by Paul at 2007-12-14 15:19
Cool way of doing date formatting. I was using a for loop to do the same.

Great Script!

Posted by ThirtyWhat at 2008-01-08 11:22
Worked like a charm ... thanks, Anne!

Very helpful, thank you

Posted by Anon at 2008-01-25 13:34
I had wanted to know how to parse variables in command files for years, this was very helpful. Hear's another tip that may help someone. You can save and use system variables in your command files. For example, this example switches to another directory and then back to the original directory.

set orig_dir=%cd%
cd \some_other_dir
cd %orig_dir%

Dates in file names

Posted by Paul Bankston at 2008-02-04 13:49
I need to put a date in a file name which I know how to do. The problem is that I need the date to be 7 days prior to the current date.

date time stamp file names

Posted by Michael at 2008-03-21 03:18
What about before 1 A.M.? Your script works fine except between
the hours of 00:00:00 and 01:00:00.

Thank you.

Posted by Sethide at 2008-04-02 11:56
Thank you so much, I have been looking for this for a while.

Perfect, just what we needed

Posted by Dugald Holmes at 2008-04-14 22:49
Thanks Anne - this was exactly what we needed to automate filenames to include the day's date.

We used (on an Australian regional settings XP server):
SET newfilename=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%

And then wrote to the file via pipe e.g. scripted output > %filename%.txt
Cheers, Dugald.

Date Time Stamp

Posted by John at 2008-04-29 17:39
Could not get my system to recognize the date part of the script. Only the MONTH part worked!!

set YY=%date:~2,2%
set MM=%date:~0,2%
set DD=%date:~7,2%

Defining time worked fine.

used dos date & time script

Posted by carole at 2008-05-02 12:25
worked like a charm - thanksss

timestamp

Posted by Jared at 2008-05-08 14:06
Great tip, thanks.

I had forgotten that environment variables could be manipulated this way.

For anyone interested, here's timestamp:

set TIMESTAMP=%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%

C:\win32\timestamp>echo %TIMESTAMP%
2008_05_08_11_02_03

Timestamp ex. 20080510_082233

Posted by alby at 2008-05-10 02:54
This my solution: ex. 20080510_082233

set timestamp=%time:~0,8%
if @%timestamp:~0,1% == @ set timestamp=0%timestamp:~1,7%
set timestamp=%date:.=%_%timestamp::=%

echo.Timestamp :: %timestamp%

syntax incorrect

Posted by maahony at 2008-07-07 15:35
I copied the original code:
REM Create sub directory called \yymmdd_hhmmss
REM where yymmdd_hhmmss is a date_time stamp like 030902_134200
set hh=%time:~0,2%
REM Since there is no leading zero for times before 10 am, have to put in
REM a zero when this is run before 10 am.
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
md C:\%yymmdd_hhmmss%

and modified only the drive path and when run the file generates the following error:
The syntax of the command is incorrect.

Any ideas? Seems like this worked for everyone else quite well.

Thank you

Posted by Someone in need at 2008-07-14 03:30
Life saving script for me. Thanks a bunch.

Thank you

Posted by Sohail at 2008-07-22 10:37
Thank you very much for reminding me of the joy of smart batch files, you saved me a lot of work today.

You made my day!!

Posted by Rik at 2008-08-11 10:19
Not only you saved me two hours of research, you helped me set a standard in my code. I appreciate it Anne!!

Thank You!

Posted by motely at 2008-08-20 14:16
Finally, I found a script that works. I have been searching for this answer for days!!!
Thank you so much!

Thanks

Posted by Ton (NL) at 2008-08-26 07:57
Thanks for the good script

Great Code

Posted by Sudhakara.T.P. at 2008-09-22 09:55
Hi,
Thank you very much for the wonderful code.

That worked like a champ.

-Regards,
Sudhakara.T.P.

This saved my day

Posted by Dola Chandra at 2008-09-24 11:49
Thanks Anne, This script saved my day. I am looking for this kind of script, you have exactly what I needed.
Anne Gentle

Subscribe to Anne's blog Subscribe to Anne's blog

Anne Gentle

Bio & Writings

Email Alert: Anne's Blog

Get an email alert when I publish a new blog entry!  Enter your email address:

 

Powered by Plone

This site conforms to the following standards: