Windows Batch File (.bat): Get Current Date as MMDDYYYY

   Posted July 24, 2005 by Jimmy Selix in Computer programming

I discovered the following tech-recipe while working on a some old dos batch files. This tutorial describes a simple .bat file used to create a folder based on the date in the MMDDYYYY format.


This .bat file is useful for scripters who need to use the current date variable in the format MMDDYYYY.
First, copy and paste the text in quotes to a text file. Then rename the file test.bat, and run it from the command line to view it. (Echo is on by default.)

The steps in this tutorial can be used in a powerful way. The FOR command can be reused and changed to query to see if a file exists. If so, you can create the folder, etc.

For now, let’s start with the basics.

    echo on
    @REM Seamonkey’s quick date batch (MMDDYYYY format)
    @REM Set ups %date variable
    @REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY

    FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE/T’) DO SET CDATE=%%B
    FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘DATE/T’) DO SET mm=%%B
    FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %CDATE%’) DO SET dd=%%B
    FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
    SET date=%mm%%dd%%yyyy%

This does nothing but set up the %date variable to be today’s date in MMDDYYYY format so that it can be called later in the script, etc.

 

About Jimmy Selix

Jimmy Selix is an early adopter that loves to be one of the first on the block to have the latest and greatest in technology and gadgets. Another love of his is being able to share his knowledge to others seeking it. Feel free to drop any comments or questions that you may have.
View more articles by Jimmy Selix

The Conversation

Follow the reactions below and share your own thoughts.

Comments are closed.