Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Windows Tutorials»How To Create Filename with Date Time in Windows Batch Script

    How To Create Filename with Date Time in Windows Batch Script

    By RahulApril 15, 20232 Mins Read

    This tutorial will help you to create files and directories with the name of the current date-time on the Windows system. For example, you are writing a script that creates backup regularly, Now you want to organize daily backups with the current date and time name, so it will be easier to identify, which folder containers backups of which date.

    Let’s go through the tutorial and understand the process to accomplish this.

    Get Date & Time in Batch Script

    Windows takes the date in the format like Sat 04/15/2023. So use following commands to extract the date in YYYY format, month in MM format and date in DD format and stored in CUR_YYYY, CUR_MM, and CUR_DD variables correspondingly.

    1
    2
    3
    set CUR_YYYY=%date:~10,4%
    set CUR_MM=%date:~4,2%
    set CUR_DD=%date:~7,2%

    Next is to parse the time which is available in 14:53:06.80 (Hours, Minutes, Seconds, and Micro Seconds) format. Now extract the hours, minutes, seconds, and microseconds and store them in variables.

    1
    2
    3
    4
    5
    6
    set CUR_HH=%time:~0,2%
    if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
     
    set CUR_NN=%time:~3,2%
    set CUR_SS=%time:~6,2%
    set CUR_MS=%time:~9,2%

    Now, you have variables having current date and time in variables. You can use and create any file name as per your requirements like:

    1
    2
    3
    set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
    mkdir %SUBFILENAME%
    echo "Welcome Here!" > example_%SUBFILENAME%.log

    If your current date time is Apr 15, 2023 14:53:06, then the above example will create a file in the current directory with name “example_20230415-145306.log”.

    A Sample Batch Script with Date & Time

    The complete windows batch script will look like below. To test this create a file script.bat with the following content.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    echo off
    set CUR_YYYY=%date:~10,4%
    set CUR_MM=%date:~4,2%
    set CUR_DD=%date:~7,2%
    set CUR_HH=%time:~0,2%
    if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
     
    set CUR_NN=%time:~3,2%
    set CUR_SS=%time:~6,2%
    set CUR_MS=%time:~9,2%
     
    set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
    mkdir %SUBFILENAME%
    echo "Welcome Here!" > access_%SUBFILENAME%.log

    Save the file and close it.

    Open a terminal and execute the above batch script.

    Creating File and Directory Using Current Date/Time in Windows Batch Script
    File and Directory with Date and Time

    You will find that a directory is created with the name “20230415-145306”. Also, a file created in the current directory with the name “example_20230415-145306.log” (Filename will be according to current date and time and will change during your testing)

    Conclusion

    In conclusion, the tutorial outlines a straightforward and efficient approach to creating filenames with date and time stamps using Windows batch scripts. This technique is highly beneficial for users looking to automate tasks, manage files, and maintain organization in their file systems.

    batch datetime date datetime time
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Python Program to Convert String to Datetime

    Setting and Getting the Default Timezone in Python

    Converting UTC Date and Time to Local Time in Linux

    View 27 Comments

    27 Comments

    1. Scott on December 19, 2022 3:47 pm

      This script with modified date vs current date is just what I need.

      Reply
    2. Niño on December 5, 2022 1:58 am

      How about 12-hour only?

      Reply
    3. Ivan on November 12, 2022 10:51 pm

      Thank you so much for sharing your script with us.

      I am using it to generate differential backups using the command-line version of 7-ZIP.

      Reply
    4. Gaurav Shrivastava on May 19, 2022 8:00 am

      How to make a folder in a specified directory. I used a task scheduler to automate the folder creation, however, it is creating the folder in C:/Windows.

      Reply
      • Rahul on May 20, 2022 12:12 pm

        Hi Gaurav, You can add the folder location as prefix to mkdir command:


        mkdir D:\\parrent-dir\\%SUBFILENAME%

        Reply
    5. Tim on May 17, 2022 9:56 am

      Doesn’t work at all for me as posted. Mainly because my date variable is in the format 17/05/2022 . No day of week, and (unlike those weird Americans, who are just awkward for the sake of it – what’s next, time expressed as mm:hh:ss ? ) we have our dates as DD/MM/YYYY in the UK.

      Windows formats dates differently for everyone, depending on location settings etc. Another top tip is to use an example date with DD over 12 (instead of 2nd November try 21st November), so we can clearly see which bit is the month and which the days.

      Maybe you should suggest readers echo their %date% variable to start with, so they can find out what it looks like. A little knowledge is a dangerous thing.

      Reply
    6. Ashfaque on November 23, 2021 6:15 am

      Excellent, Very useful

      Reply
    7. John on September 23, 2021 8:53 pm

      If you want the file to be inside the directory you created then the last line needs to be
      echo “Welcome Here!”>%fecha1%\access_%fecha1%.log

      Reply
    8. John on September 23, 2021 8:51 pm

      There is no need for the line
      SET fechaTIME1=%time:~0,2%%time:~3,2%%time:~6,2%

      Reply
    9. John on September 23, 2021 8:42 pm

      That last example works well apart from the quotation marks around Welcome Here! create odd characters so I removed them and if you intended for the text file to go in the folder created than you need to change the last line to
      echo “Welcome Here!”>%fecha1%\access_%fecha1%.log

      Reply
    10. Rick on August 24, 2021 8:02 pm

      I’m having a hard time figuring out how a Date/Time like Nov 02, 2017 15:41:36 can come up with a file name like 20170306-143822

      Reply
      • Rahul on August 25, 2021 4:29 am

        Hi Rick, Thanks for pointing out. I have corrected the article.

        Reply
    11. Daniel Adeniji on August 11, 2021 9:04 pm

      Rahul Kumar:-

      Thanks for sharing this.

      It is a very clean and precise code snippet for getting the current timestamp in a DOS Batch file.

      Daniel Adeniji

      Reply
    12. Christopher Baranski on November 27, 2020 3:13 pm

      Works Great – Thanks

      Reply
      • Christopher Baranski on November 27, 2020 3:16 pm

        oh, I used your environment variables to make a Win 10 filename, the last line just echos command line to test! Thanks again!

        echo off
        set CUR_YYYY=%date:~10,4%
        set CUR_MM=%date:~4,2%
        set CUR_DD=%date:~7,2%
        set CUR_HH=%time:~0,2%
        if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)

        set CUR_NN=%time:~3,2%
        set CUR_SS=%time:~6,2%
        set CUR_MS=%time:~9,2%

        set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%

        echo “ffmpeg -f image2 -framerate 15 -i SC-%%07d.jpg -s 640×360 SC-Movie-%SUBFILENAME%.avi”

        Reply
    13. Merlinox on May 19, 2020 7:55 am

      Great, but your date script doesn’t work (win 10).
      I used:
      set ToDaysDate=%date:~6,4%%date:~3,2%%date:~0,2%
      set CUR_NN=%time:~3,2%
      set CUR_SS=%time:~6,2%
      set CUR_MS=%time:~9,2%
      set SUBFILENAME=%ToDaysDate%-%CUR_HH%%CUR_NN%%CUR_SS%

      Reply
      • WonderWorker on March 3, 2021 10:25 am

        It’s a common mistake to make. You reference %CUR_HH% but you haven’t initialised it yet. Initialise it as follows:

        set CUR_HH=%time:~0,2%
        if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)

        Reply
    14. Jody L Meyer on February 17, 2020 2:08 pm

      Thank you! It works great for me!

      Reply
    15. Eric on September 12, 2019 3:51 am

      Thanks so much for this, tested on win10 pro and it works perfectly.

      Reply
    16. Keith on June 21, 2019 12:21 pm

      Windows 10 as follows:
      set CUR_YYYY=%date:~6,4%
      set CUR_MM=%date:~3,2%
      set CUR_DD=%date:~0,2%
      set CUR_HH=%time:~0,2%
      if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)

      set CUR_NN=%time:~3,2%
      set CUR_SS=%time:~6,2%
      set CUR_MS=%time:~9,2%

      set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
      mkdir %SUBFILENAME%
      cd %SUBFILENAME%
      echo “Welcome Here!” > access_%SUBFILENAME%.log

      Reply
    17. Ash on March 27, 2019 11:11 pm

      The string pointers for the Day and the Month are wrong
      it should be
      set CUR_MM=%date:~7,2%
      set CUR_DD=%date:~4,2%

      Reply
    18. am on March 21, 2019 5:33 pm

      The solution above breaks if you change locale. See https://serverfault.com/questions/227345/locale-unaware-date-and-time-in-batch-files

      Reply
    19. Eder Silva on March 6, 2019 3:28 pm

      how to generate the day of the week?

      Reply
    20. SCS on March 6, 2019 12:06 pm

      Thank you very much, was very helpful, BUT:

      – you should mention that the date and time string is depending on the format at the local settings of windows.
      – people should adjust the extraction of the string, as extra help:
      set D=1234567890
      set A=%D:~4,2% gives ’56’ , so 2 char AFTER the 4.
      -it is better to first save the string to a variable instead of using the %date% and %time% again.
      for example: if I use your code on Nov 30, 2017 on 23:59, say 0,002sec before midnight, the last lines could be executed on the next day: 20171101 , so you miss a month!
      More a problem with the time, when you want precision 03,98sec might become SS 03 + MS 05 = 03,05 in stead of the expected 03,98 or 4,05
      So start with :
      set D=%date%
      and change to :
      set CUR_MM=%D:~4,2%

      Just my 2 cents, hope it helps.
      SCS

      Reply
    21. Vladimir on January 7, 2019 3:35 pm

      Hello Rahull,

      this script is fine, work for me, but: now is date 07.01.2019 today, and %CUR_DD% is 01 instead 07. Why? What is wrong?
      I testing on Win 10.

      Reply
    22. Joao on January 3, 2019 4:05 pm

      windows 10 -> sysntax error

      Reply
      • Jorge on July 4, 2019 7:20 pm

        SET fechaTIME=%time:~0,2%:%time:~3,2%:%time:~6,2%
        SET fechaTIME1=%time:~0,2%%time:~3,2%%time:~6,2%
        SET fechaDATE=%date:~6,4%%date:~3,2%%date:~0,2%
        SET fecha1=%fechaDATE%-%fechaTIME1%
        echo on

        mkdir %fecha1%
        echo “Welcome Here!”>access_%fecha1%.log

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    • Variable Expansion in ZSH
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.