Thursday, June 3, 2010

Create a date and time stamp in your batch files

Steve Wiseman's blog has a very helpful article about creating a date and time stamp in batch files, here is the link and brief summary:
1 Link:
http://www.intelliadmin.com/index.php/2007/02/create-a-date-and-time-stamp-in-your-batch-files/

2 Brief summary:

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
For timestamp, use:
%time:~-11,2%%time:~-8,2%%time:~-5,2%

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home