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

In batch scripting, generating file and directory names with current date and time stamps can greatly enhance organization and workflow. This tutorial will walk you through various code snippets and techniques to format date and time in a batch script. By incorporating these methods, you can efficiently create time-stamped files and directories according to your desired format.

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

Method 1: Extracting Date Components

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: =%

Method 2: Regular Expressions for Date Manipulation

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%

Method 3: Extracting Date Components with Substring Operations

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

Section 2: Formatting Time in Batch Script

Method 1: Parsing Hour and Minute Components

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%

Method 2: Utilizing the DATE /T Command

for /f "tokens=1-2 delims=: " %%j in ('time /t') do (
set hh=%%j
set mn=%%k
)
set MYTIME=%hh%%mn%

Section 3: Combining Date and Time To merge the date and time strings obtained from the previous sections, you can use concatenation to form custom file and directory names that include both the date and time stamps.

Conclusion: By mastering the techniques shared in this tutorial, you can now create batch scripts that generate file and directory names with accurate and formatted date and time stamps. Incorporating these methods will streamline your workflow and enhance organization, ensuring that your files and directories are properly labeled with time-stamped information. Use these code snippets to enhance your batch scripting projects and boost productivity.

4 comments
  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 to Saiful Cancel reply

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