Previous Posts


Verify Install of Windows Daylight Saving Patch

Windows 98, ME DST Patch

New vulnerability discovered in Excel

Active Sync Released For Vista

The many faces of Windows

Tweak your IE 7 Settings

The 5 sins of Vista

Disable USB Flash Drives

Reduce Word 2007 Compatibility Problems

Tweak your recent documents list



Archives

May 2005

January 2006

April 2006

May 2006

June 2006

July 2006

September 2006

October 2006

November 2006

December 2006

January 2007

February 2007

March 2007

April 2007

May 2007

June 2007

July 2007

August 2007

September 2007

October 2007

November 2007

December 2007

January 2008

February 2008

March 2008

April 2008

May 2008

June 2008

July 2008

August 2008

September 2008

October 2008

November 2008



Subscribe to our Feed:






Create a date and time stamp in your batch files

If you like this article, then sign up for our email newsletter to get more like it every day in your inbox

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.


Posted By: Steve Wiseman on Thursday, February 15, 2007

Check out our utilities for windows

 



Copyright © IntelliAdmin, LLC, 2008. All Rights Reserved