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. 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. Now, you have variables having current date and time in variables. You can use and create any file name as per your requirements like: 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. 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.

How to Create Filename with Date Time in Windows Batch Script   TecAdmin - 72