How to Create a File/Folder and Name it based on Current Timestamp

If you are looking for ways to automatically creates files and folders and name them based on your system’s timestamp, you have come to the right place. There is a super simple method using which you can get this task done. The folders or files created can then be used for various purposes like storing the file backups, sorting files based on dates etc.

In this article, we explain in some very simple steps how you can automatically create files and folders in your Windows 11/10 and name them based on the system’s timestamp. The method used is batch scripting and it is very easy. Hope you enjoy reading the article.

 

11 File Created Min

 

Section 1: How to Automatically Create a Folder and Name it based on System’s Current Timestamp

Step 1: Firstly, navigate to the parent folder where you want to create the folder and name it based on the system’s current timestamp.

As next, right click on an empty space, click on New and then click on the Text Document option.

 

1 New Text Min

 



Step 2: Now double click on the newly created text document to edit it.



 

2 Open File Min

 

Step 3: Once the text document opens up in Notepad, copy and paste the following script onto it.

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_DD%-%CUR_MM%-%CUR_YYYY%_%CUR_HH%.%CUR_NN%.%CUR_SS%
mkdir %SUBFILENAME%

 

3 Copy Script Min

 

Don’t forget to hit the keys CTRL + S together to save the file once you are done copying the script above.

Script Explanation

The script first extracts the current day, month, year, hours, minutes, seconds and milli seconds from the system time. The script responsible for that part is below.

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%

So the variables created are as follows:

CUR_YYYY – Stores the year

CUR_MM – Stores the month

CUR_DD – Stores the day

CUR_HH – Stores the hours

CUR_NN – Stores the minutes

CUR_SS – Stores the seconds

CUR_MS – Stores the milli seconds

The below line is the one responsible for formatting the name of the folder. As per the below line, the name of the folder will be in the format Day-Month-Year_Hours.Minutes.Seconds. The format is then saved in a variable named SUBFILENAME.

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

Finally, the folder is created using the mkdir command.

mkdir %SUBFILENAME%

How to Tweak the Naming Format

  • If you need a different format for naming your folder, you can use the variables explained in the section above. For example, if you would like the format of your folder name to be like Year_Month_Day-Seconds.Hours.Minutes, then your set SUBFILENAME line will have to be changed as follows.
     set SUBFILENAME=%CUR_YYYY%-%CUR_MM%-%CUR_DD%_%CUR_SS%.%CUR_HH%.%CUR_NN%

Result ==> 2022-04-15_58.21.15

  • You can also change the separator between the variables. For example, if you want hyphens to separate the time as well instead of dots, then your SUBFILENAME will have to be changed to the following.
     set SUBFILENAME=%CUR_DD%-%CUR_MM%-%CUR_YYYY%_%CUR_HH%-%CUR_NN%-%CUR_SS%

Result ==> 15-04-2022_21-18-26

  • If you want no separators between Date elements and Time elements, but need a hyphen between date and time, then SUBFILENAME will be:
    set SUBFILENAME=%CUR_DD%%CUR_MM%%CUR_YYYY%_%CUR_HH%%CUR_NN%%CUR_SS%

Result ==> 15042022_211849

Step 4: As next, come back to the folder where you have saved your text document, click on it and then press the F2 key to rename it.

Give a name of your choice, but you have to give the extension as bat. This is the most important part.

 

4 Rename Min

 

Step 5: Once you rename and click somewhere else, you will be presented with the Rename confirmation dialog box. Click on the Yes button to proceed to the next step.

 

5 Confirm Rename Min

 

Step 6: Your batch script is now ready to be executed. Double click on the file to execute it.

 

6 Execute Script Min

 

Step 7: Magic! A new folder is created inside the same folder as your batch script and its naming is based on your system’s current timestamp.

 

7 Folder Created Min

 

Section 2: How to Automatically Create a File and Name it based on System’s Current Timestamp

In Section 1, we created a folder that was named based on the system’s current timestamp. In this section, let’s see how you can create a file automatically and name it based on the system’s current timestamp.

First of all, create the batch file as detailed in Section 1.

Step 1: Right click on the batch file you created from Section 1 and then click on Show more options.

 

8 Show More Options Min

 

Step 2: From the menu that expands out, click on the Edit option.

 

9 Edit Optimized

 

Step 3: Now, comment out the mkdir line towards the end. This is the part of the script that is responsible for making the folder.

To comment out a line in batch scripting, you need to add 2 colons to the beginning of the line. This would make the script ignore the line following the colons. So, your mkdir line would be as follows and it will be ignored during script execution.

::mkdir %SUBFILENAME%

Now, let’s add the line that will create the file, using the same naming format.

echo "Hello, Welcome to The Geek Page" > %SUBFILENAME%.txt

So, the final code that needs to be present in the batch script file should be as follows.

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_DD%%CUR_MM%%CUR_YYYY%_%CUR_HH%%CUR_NN%%CUR_SS%
::mkdir %SUBFILENAME%

echo "Hello, Welcome to The Geek Page" > %SUBFILENAME%.txt

 

9 Create File Command Min

 

Don’t forget to save the file by pressing the CTRL and S keys together as always.

Step 4: Double click on your batch script to execute it.

 

10 Execute New Batch Min

 

Step 5: There you go! Now a new file is created with the default text Hello, Welcome to The Geek Page. You can double click on the text file to open it. You can edit the file and add any text as per your choice, just like how you would normally edit and save a text file. Enjoy!

 

11 File Created Min

 

Section 3: How to Automatically Create a Folder and a File and Name them based on System’s Current Timestamp

In this section, once you double click on the batch file, a file and a folder will be created automatically, and they will both be named based on the system’s current timestamp.

Step 1: Right click on the batch script you created in Section 2, and then click on Show more options.

 

12 Show More Options Copy Min

 

Step 2: Click on the Edit option in the next step.



 

9 Edit Optimized

 

Step 3: To create the folder as well, along with the file, remove the :: from the beginning of the mkdir line.

Your final script should be as follows.

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_DD%%CUR_MM%%CUR_YYYY%_%CUR_HH%%CUR_NN%%CUR_SS%
mkdir %SUBFILENAME%

echo "Hello, Welcome to The Geek Page" > %SUBFILENAME%.txt

 

13 Uncomment Mkdir Min

 

As always, save the file by pressing the CTRL + S keys simultaneously.

Step 4: Double click on the batch file to execute, once you have saved it.

 

14 Execute Batch Min

 

Step 5: Voila! You can see that a new file and a folder are now created and they are both named based on your system’s current timestamp.

 

15 Folder And File Created Min

 

Please tell us in the comments section if you are stuck at any of the steps.

Stay tuned for more tricks, tips and fixes.