Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    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

    RahulBy RahulOctober 21, 20172 Mins ReadUpdated:August 25, 2021

    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 Thu 11/02/2017. 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.

    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 15:41:36.39 (Hours, Minutes, Seconds, and Micro Seconds) format. Now extract the hours, minutes, seconds, and microseconds and store them in variables.

    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:

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

    If your current date time is Nov 02, 2017 15:41:36, then the above example will create a file in the current directory with name “access_20171102-154136.log”.

    A Sample Batch Script with Date & Time

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

    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
    

    Open a terminal and execute the above batch script.

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

    Conclusion

    In this tutorial, you have learned to create directories or filenames based on the current date time in the Windows system.

    batch datetime date datetime time
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install TeamViewer 15 on Linux
    Next Article How to Check If Certificate, Private Key and CSR Match

    Related Posts

    How to Enable / disable Firewall in Windows

    Updated:May 17, 20222 Mins Read

    How to Start & Stop Windows Service via Command Line

    Updated:March 29, 20222 Mins Read

    How to Change Windows Hostname (Computer Name)

    Updated:March 11, 20223 Mins Read

    How To Install NVM on Windows

    Updated:April 16, 20223 Mins Read

    How to Check Computer Uptime in Windows

    3 Mins Read

    How To Enable Multiple RDP Sessions on Windows Server

    Updated:May 17, 20222 Mins Read

    23 Comments

    1. 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
    2. Ashfaque on November 23, 2021 6:15 am

      Excellent, Very useful

      Reply
    3. 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
    4. 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
    5. 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
    6. 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
    7. 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
    8. 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
    9. 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
    10. Jody L Meyer on February 17, 2020 2:08 pm

      Thank you! It works great for me!

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

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

      Reply
    12. 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
    13. 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
    14. 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
    15. Eder Silva on March 6, 2019 3:28 pm

      how to generate the day of the week?

      Reply
    16. 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
    17. 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
    18. 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

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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