Windows Batch File (.bat): Get Current Date as MMDDYYYY
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.
@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
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.
View more articles by Jimmy Selix
Follow the reactions below and share your own thoughts.