If you are looking for a way to automatically create and name files and folders based on system timestamps, you have come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc.
In this article, we will explain how to automatically create files and folders in Windows 11/10 and name them based on the system’s timestamp in some very simple steps. The method used is a batch script, which is very simple. Hope you enjoyed reading this article.
Step 1: First, Navigate to the parent folder where you want to create the folder and name it based on the system's current timestamp.
Next, right-click the empty area, click New, and then click the Text Document option.
Step 2: Nowdouble-clickthe newly created text document to edit it.
Step 3: After opening the text document in Notepad, copy and paste the following script onto it.
回声设置 CUR_YYYY=%date:~10,4%设置 CUR_MM=%date:~4,2%设置 CUR_DD=%date:~7,2%设置 CUR_HH=%time:~0,2%如果 %CUR_HH% lss 10(设置 CUR_HH=0%time:~1,1%)设置 CUR_NN=%time:~3,2%设置 CUR_SS=%time:~6,2%设置 CUR_MS=%time:~9,2%设置 SUBFILENAME=%CUR_DD%-%CUR_MM%-%CUR_YYYY%_%CUR_HH%.%CUR_NN%.%CUR_SS%mkdir %SUBFILENAME%
Don’t forget to press the CTRL S key at the same time to save the file.
Script description
This script firstextracts the current day, month, year, hour, Minutes, seconds and milliseconds. The script responsible for this part is as follows.
设置CUR_YYYY =%date:~10,4%设置CUR_MM =%date:~4,2%设置CUR_DD =%date:~7,2%设置CUR_HH =%time:~0,2%如果 %CUR_HH% lss 10(设置 CUR_HH=0%time:~1,1%)设置CUR_NN =%time:~3,2%设置CUR_SS =%time:~6,2%设置CUR_MS =%time:~9,2%
CUR_YYYY – Store the year CUR_MM – Store the month CUR_DD – Store the day CUR_HH – Storage hours CUR_NN – Storage minutes CUR_SS – Storage seconds CUR_MS – Storage millisecondsThe following lines are The line responsible for formatting the folder name. Based on the following lines, 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.
设置 SUBFILENAME=%CUR_DD%-%CUR_MM%-%CUR_YYYY%_%CUR_HH%.%CUR_NN%.%CUR_SS%
Finally, use the mkdir command to create the folder.
mkdir %SUBFILENAME%
How to adjust the naming format
设置 SUBFILENAME=%CUR_YYYY%-%CUR_MM%-%CUR_DD%_%CUR_SS%.%CUR_HH%.%CUR_NN%
You can also change the separator between variables. For example, if you want the
设置 SUBFILENAME=%CUR_DD%-%CUR_MM%-%CUR_YYYY%_%CUR_HH%-%CUR_NN%-%CUR_SS%
设置 SUBFILENAME=%CUR_DD%%CUR_MM%%CUR_YYYY%_%CUR_HH%%CUR_NN%%CUR_SS%
Step 4
: Next, go back to the folder where you saved the text document, click it and press F2 Keyrenameit. Provide a name of your choice, but you must specify the
extensionas bat. This is the most important part.
Step 5: After renaming and clicking elsewhere, you will see the Rename Confirmation dialog box. Click the Yes button to proceed to the next step.
Step 6: Your batch script is now ready to execute. Double-click the file to execute it.
第7步:魔术!将在与批处理脚本相同的文件夹内创建一个新文件夹,其命名基于系统的当前时间戳。
在第 1 节中,我们创建了一个基于系统当前时间戳命名的文件夹。在本节中,让我们看看如何根据系统当前的时间戳自动创建文件并为其命名。
首先,创建第 1 节中详述的批处理文件。
第 1 步:右键单击您从第 1 节创建的批处理文件,然后单击显示更多选项。
第 2 步:从展开的菜单中,单击“编辑”选项。
第 3 步:现在,注释掉最后的mkdir 行。这是负责制作文件夹的脚本部分。
要在批处理脚本中注释掉 一行,您需要在该行的开头添加2 个冒号。这将使脚本忽略冒号后面的行。因此,您的 mkdir 行将如下所示,并且在脚本执行期间将被忽略。
::mkdir %SUBFILENAME%
现在,让我们使用相同的命名格式添加将创建文件的行。
echo "你好,欢迎来到极客页面" > %SUBFILENAME%.txt
因此,需要出现在批处理脚本文件中的最终代码应如下所示。
回声设置 CUR_YYYY=%date:~10,4%设置 CUR_MM=%date:~4,2%设置 CUR_DD=%date:~7,2%设置 CUR_HH=%time:~0,2%如果 %CUR_HH% lss 10(设置 CUR_HH=0%time:~1,1%)设置 CUR_NN=%time:~3,2%设置 CUR_SS=%time:~6,2%设置 CUR_MS=%time:~9,2%设置 SUBFILENAME=%CUR_DD%%CUR_MM%%CUR_YYYY%_%CUR_HH%%CUR_NN%%CUR_SS%::mkdir %SUBFILENAME%echo "你好,欢迎来到极客页面" > %SUBFILENAME%.txt
不要忘记像往常一样同时按下CTRL 和 S键来保存文件。
第 4 步:双击您的批处理脚本以执行它。
第5步:你去!现在使用默认文本Hello, Welcome to The Geek Page创建了一个新文件。您可以双击文本文件将其打开。您可以编辑文件并根据您的选择添加任何文本,就像您通常编辑和保存文本文件的方式一样。享受!
在本节中,双击批处理文件后,将自动创建一个文件和一个文件夹,它们都将根据系统当前的时间戳命名。
第 1 步:右键单击您在第 2 节中创建的批处理脚本,然后单击显示更多选项。
第 2 步:单击下一步中的“编辑”选项。
第 3 步:要创建文件夹以及文件,请从mkdir行的开头删除:: 。
您的最终脚本应如下所示。
回声设置 CUR_YYYY=%date:~10,4%设置 CUR_MM=%date:~4,2%设置 CUR_DD=%date:~7,2%设置 CUR_HH=%time:~0,2%如果 %CUR_HH% lss 10(设置 CUR_HH=0%time:~1,1%)设置 CUR_NN=%time:~3,2%设置 CUR_SS=%time:~6,2%设置 CUR_MS=%time:~9,2%设置 SUBFILENAME=%CUR_DD%%CUR_MM%%CUR_YYYY%_%CUR_HH%%CUR_NN%%CUR_SS%mkdir %SUBFILENAME%echo "你好,欢迎来到极客页面" > %SUBFILENAME%.txt
与往常一样,同时按CTRL + S键保存文件。
第4步:保存后双击批处理文件执行。
第5步:瞧!您可以看到现在创建了一个新文件和一个文件夹,它们都根据您系统的当前时间戳命名。
The above is the detailed content of How to create and name a file/folder based on current timestamp. For more information, please follow other related articles on the PHP Chinese website!