fortypoundhead.com

Grabbing System Date in a Batch File

Posted On 2012-05-04 by dwirch
Keywords:
Tags: Windows Commandline Scripting Tutorial Windows
Views: 959


Batch programming can be a powerful thing. Basically, anything you can run at a command prompt can be run in a batch file. Coupled with task scheduler, process automation nirvana can be achieved.

However, one thing that is conspicuously is the presence of a date or time variable. For example, if you wanted to date or time encode a filename or create a log entry, you have no way to do that in native batch. But there is a way!

There is a DATE command is available though. With a simple bit of trickery, we can grab that info and store it in a variable, then use it however we want within our script.

The actual code is posted at the bottom of this article, but I want to give a basic flow/explanation of what is going on with it first. Because knowledge is power, that's why. And not everyone is a seasoned IT professional.

  • First, we need to decide if the DATE command is available. Some administrators may limit this command for users. Not sure why, but it happens.
    • First we echo the date and look for the string mm.
    • If the string doesn't exist, the command must not be available, so exit the script.
    • Otherwise, continue to grab the date info.
  • Assuming the date is available, we'll run the DATE /T command. and step through it to find certain delimiters, namely the slash (/)
  • For each delimiter we find the necessary value of the year, month or day, and store it in a variable (YYYY, MM, or DD)
  • After all pieces are found, they are combined into a single variable, DateStamp

Pretty straightfoward, eh? At the bottom of the code, there is a small section on how to use the variable in some file operations, such as creating a directory or copying a file.

Anyway, here is the code, watch out for line wrap. Hope this helps!

.echo off
echo *** Finding DateStamp YYYYMMDD

REM ***
REM *** Echo date to null. If the date command is not available, exit the script
REM ***

echo. | date | FIND "(mm" > NUL
If errorlevel 1,(call :Parsedate DD MM) Else,(call :Parsedate MM DD)
goto :ScriptDone

:Parsedate ----------------------------------------------------------

REM ***
REM *** The date is available. Parse the output of DATE /T
REM ***

For /F "tokens=1-4 delims=/.- " %%A in ('date /T') do if %%D!==! (set %1=%%A&set %2=%%B&set YYYY=%%C) else (set DOW=%%A&set %1=%%B&set %2=%%C&set YYYY=%%D)
(Set DateStamp=%YYYY%%MM%%DD%)

REM ***
REM *** Example usage of DateStamp variable
REM ***

echo *** Datestamp:%DateStamp%

REM ***
REM *** Sample usage of the variable
REM ***

REM *** first, create a directory with the datestamp pre-pended to the directory name

mkdir k:\backup\%DateStamp%-webroot

REM *** copy a file with the date in the filename

copy c:\myfile.txt k:\backup\%DateStamp%_myfile.txt

:ScriptDone


About the Author

dwirch has posted a total of 172 articles.

 


Comments On This Post

By: dwirch
Date: 2013-08-25

Anyone notice the bug in the code above?


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.162.19.123

Before you can post, you need to prove you are human. If you log in, this test goes away.


Beat With A Stick List Returns: By request, BWASL has returned to the site. Free your rage by telling the world about something that really grinds your gears. Add your beating.



Recent Forum Posts

BWASL returns
dwirch posted on May 13, 2017 at about 15:24 in Site News

BWASL returns
dwirch posted on May 13, 2017 at about 8:46 in Site News

Job Spammer: Balashankar Bose Bose
dwirch posted on May 11, 2017 at about 10:05 in Spammers

Job Spammer: Bharti Jigyasi
dwirch posted on May 11, 2017 at about 7:58 in Spammers

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers