18 Nov

bat – batch file to create formatted date time (ddmmyyyy) directory

I required to make a batch script which needs file / directory to be created with current date / time stamps.
Following are the code snipts for formating date / time in a batch script:

FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE/T’) DO SET MYDATE=%%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 %MYDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/” %%A IN (‘echo %MYDATE%’) DO SET yyyy=%%B
set DATED=%mm%%dd%%yyyy%
md %DATED: =%

or

FOR /F “tokens=*” %%A IN (‘DATE/T’) DO SET MYDATE=%%A
#Using regular expresions to remove / and DAY
SET MYDATE=%MYDATE:/=%
SET MYDATE=%MYDATE:* =%
md %MYDATE%

Other ways to achieve date string in a batch script variable:

@echo off
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set MYDATE=%yy%%mm%%dd%

or

for /f “tokens=2-4 delims=/ ” %%g in (‘date /t’) do (
set mm=%%h
set dd=%%g
set yy=%%i
)
set MYDATE=%yy%%mm%%dd%

Similarly we can work with time:

for /f “tokens=1-2 delims=: ” %%j in (‘time /t’) do (
set hh=%%j
set mn=%%k
)
set MYTIME=%hh%%mn%

Further you can go ahead mixing time with date as well.

4 thoughts on “bat – batch file to create formatted date time (ddmmyyyy) directory

  1. Hello, I need to develop an batch file where in it will read all the dates (in dd/mm/yy format) and give me output based upon different month.

    Input : Excel File (With multiple sheets)
    Output: Excel (Preferred)

    For a quick start we can start with single sheet, and may be a workbook later.

  2. Now, how do I copy files to these newly created folders using the same batch file? Here’s my batch file so far;

    _____________________________________________________

    @echo off
    C:
    CD\
    If exist “c:\Master King Backup\” echo Master King Backup folder already exists
    If not exist “C:\Master King Backup\” echo Creating Directory
    If not exist “C:\Master King Backup\” MD “Master King Backup”
    If not exist “C:\Master King Backup\Backup” MD “C:\Master King Backup\Backup”
    If not exist “C:\Master King Backup\Working” MD “C:\Master King Backup\Working”
    CD\”Master King Backup\Working\”
    for /f “usebackq tokens=1-3” %%a in (`date /t`) do (
    for /f “delims=/ tokens=1-3” %%i in (“%%b”) do (
    for /l %%m in (1,1,50) do (
    if exist %%i-%%j-%%k-%%m (
    rem do nothing
    ) else (
    mkdir %%i-%%j-%%k-%%m
    cd %%i-%%j-%%k-%%m
    goto finis
    )
    )
    )
    )
    :finis
    rem do stuff in our directory here like copy some files
    rem copy c:\temp\*.*
    xcopy /E /C /F /R /Y “C:\Program Files (x86)\HPCSoft\MasterKing32\Database\*.*” “C:\Master King Backup\Working\”
    xcopy /E /C /F /R /Y “C:\Master King Backup\Working\” “\\readyshare\USB_Storage\Master King Backup\working\”
    CD\”Master King Backup\Backup\”
    for /f “usebackq tokens=1-3” %%a in (`date /t`) do (
    for /f “delims=/ tokens=1-3” %%i in (“%%b”) do (
    for /l %%m in (1,1,50) do (
    if exist %%i-%%j-%%k-%%m (
    rem do nothing
    ) else (
    mkdir %%i-%%j-%%k-%%m
    cd %%i-%%j-%%k-%%m
    goto finis
    )
    )
    )
    )
    :finis
    rem do stuff in our directory here like copy some files
    rem copy c:\temp\*.*
    xcopy /E /C /F /R /Y “C:\Program Files (x86)\HPCSoft\MasterKing32\Database\*.*” “C:\Master King Backup\Backup\”
    xcopy /E /C /F /R /Y “C:\Master King Backup\Working\” “\\readyshare\USB_Storage\Master King Backup\Backup\”
    _____________________________________________________

    Here’s what has to happen, when I run the batch file, I need it to create a new directory and name it (them) the current date and time. Next I need to copy some files on the local computer to the newly created directory(s). Can someone help me with this? Thank you so very much.

  3. You may also see this method given in the following location.
    <a href=”http://tips.deepumohan.com/2011/09/windows-batch-script-to-create.html” rel=”nofollow”>Windows batch script for creating directory ddmmyyy</a>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

-- Kedar Vaijanapurkar --