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.

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.
27 Comments
This script with modified date vs current date is just what I need.
How about 12-hour only?
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.
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.
Hi Gaurav, You can add the folder location as prefix to mkdir command:
mkdir D:\\parrent-dir\\%SUBFILENAME%
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.
Excellent, Very useful
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
There is no need for the line
SET fechaTIME1=%time:~0,2%%time:~3,2%%time:~6,2%
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
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
Hi Rick, Thanks for pointing out. I have corrected the article.
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
Works Great – Thanks
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”
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%
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%)
Thank you! It works great for me!
Thanks so much for this, tested on win10 pro and it works perfectly.
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
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%
The solution above breaks if you change locale. See https://serverfault.com/questions/227345/locale-unaware-date-and-time-in-batch-files
how to generate the day of the week?
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
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.
windows 10 -> sysntax error
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