Advertisement

Sunday, January 30, 2011

Changing date format to "dd-mmm-yyyy" and Making directory in DOS

                       This post describes how to change date from "mm/dd/yyyy" to "dd-mmm-yyyy" and making directory using the converted date. The default date format in the DOS is mm/dd/yyyy eg. for today is 01/30/2011. I want to change this to "dd-mmm-yyyy" format eg. for today is 30-Jan-2011. This format is widely use in Myanmar governmental offices.
                       
Default date in DOS is sun 01/30/2011  for detail go to here http://www.dostips.com/DtTipsStringOperations.php
for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set wday=%%a&set month=%%b&set day=%%c&set year=%%d
Extract month,day and year form the date string.
Just grab the Month, Day and Year. And then I will change the Month from 01,02 etc.... to Jan,Feb etc....The codes are below
IF %Month% equ 1 Set Month=Jan
IF %Month% equ 2 Set Month=Feb
IF %Month% equ 3 Set Month=Mar
IF %Month% equ 4 Set Month=Apr
IF %Month% equ 5 Set Month=May
IF %Month% equ 6 Set Month=Jun
IF %Month% equ 7 Set Month=Jul
IF %Month% equ 8 Set Month=Aug
IF %Month% equ 9 Set Month=Sep
IF %Month% equ 10 Set Month=Oct
IF %Month% equ 11Set Month=Nov
IF %Month% equ 12 Set Month=Dec
Let try to reassemble.
echo %Day%-%Month%-%Year%
It will appear in screen like 30-Jan-2011
Now Everything is changed to desired format.
 Let finish the date format
Set Date=%Day%-%Month%-%Year%
To make the directory using this customized date format
MD WHATEVER_YOU_WANT_%DATE%_WHATEVER_YOU_WANT
It will appear like
WHATEVER_YOU_WANT_30-Jan-2011_WHATEVER_YOU_WANT

For Newbies
                    Right-Click on the window and choose new text document. Copy the below code and paste into the text file and name it Cdate.bat.
@echo off
::Cdate.bat
for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set wday=%%a&set month=%%b&set day=%%c&set year=%%d
echo.Weekday: %wday%
echo.Month  : %month%
echo.Day    : %day%
echo.Year   : %year%
IF %Month% equ 1 Set Month=Jan
IF %Month% equ 2 Set Month=Feb
IF %Month% equ 3 Set Month=Mar
IF %Month% equ 4 Set Month=Apr
IF %Month% equ 5 Set Month=May
IF %Month% equ 6 Set Month=Jun
IF %Month% equ 7 Set Month=Jul
IF %Month% equ 8 Set Month=Aug
IF %Month% equ 9 Set Month=Sep
IF %Month% equ 10 Set Month=Oct
IF %Month% equ 11Set Month=Nov
IF %Month% equ 12 Set Month=Dec
echo %Day%-%Month%-%Year%
Set Date=%Day%-%Month%-%Year%::Change the below code to whatever_you_want
MD %1_%Date%_%2
::eg MD Back_%Date%
::MD WHATEVER_YOU_WANT_%DATE%_WHATEVER_YOU_WANT
::Codes are finished here

To use with a variable name
Change the code "MD WHATEVER_YOU_WANT_%DATE%_WHATEVER_YOU_WANT" to
MD %1_%Date%_%2
And Open the command window and Type Cdate.bat Backup Full
Backup is for Variable 1 and Full is for variable 2.
So it will appear like Backup_30-Jan-2011_Full
  Here is the file:Download here After running this batch file you can still use this variable date.