This post explains how to create files with unique names from batch files. We would need this when we are running some script which writes data into files and each time you need to name the file uniquely so that there’s no conflict with the previously created files.
One commonly used technique for unique names is to append the system time to the names. Let’s see how we can implement this in a Windows batch file.
This is helpful if the file is created just once per day.
Example: Let’s say you need to run dir and save the output in a file every day.
@echo off
for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set filename=%mydate:/=-%.txt
dir > %filename%
After running the above command, a new file gets created with today’s date
c:\>dir 11/15/2015 07:35 PM 2,288 11-15-2015.txt
If you need to run the script multiple times in a day, just using the date would not be enough. Adding timestamp to the file name ensures that the file name is always unique.
@echo off
for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set mydate=%mydate:/=-%
set mytime=%time::=-%
set filename=%mydate%-%mytime%.txt
dir > %filename%
After running the batch file, you would see new file created with date and time in the name.
c:\>dir 11/15/2015 07:44 PM 2,487 11-15-2015-19-44-38.48.txt
@echo off
for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set dirname=%mydate:/=-%.txt
mkdir dirname
After running the batch script, printing all directories in the folder shows the newly created folder with date in the name
c:\> dir /A:D /b 11-15-2015
Adding users from command line is much easier than going through the UI. It saves…
Do you want to delete a directory from Windows command prompt(CMD)? This post explains how…
We can kill a process from GUI using Task manager. If you want to do…
Chrome is definitely the stable browser compared to other ones and it has been my…
Windows has multiple command line utilities that can help find the version of the Windows…
There are multiple ways to check whether a Windows computer is running 32-bit OS or…
View Comments
I've used the %random% environment variable to achieve a similar effect in the past. I believe it is limited to five digits.
C:\Users\pwood>echo %random%
20948
C:\Users\pwood>echo %random%
23935
Thanks for your support for the command line, it is appreciated.
That's a good idea. Thanks Wood for sharing it.
The batch script above would generate a filename of:
04-19-2018-15-13-54.20
MM-DD-YYYY-HH-MM-SS.SS
How do I generate a file name of:
YYYY-MM-DD_HH-MM-SS
I do not want the Milliseconds. Any help would be appreciated.
Thank You,
Michael
> How do I generate a file name of: YYYY-MM-DD_HH-MM-SS ?
Try something like:
REM dtstr should yield date-time in format yyyymmddThhmmss -
REM e.g. 20110325T090446 when %date% is 25/03/2011 and %time% is 9:04:46.84
set dtstr=%date:~6,4%%date:~3,2%%date:~0,2%T%time:~0,2%%time:~3,2%%time:~6,2%
set dtstr2=%date:~6,4%-%date:~3,2%-%date:~0,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%
REM Replace blank with 0 (so e.g. " 9:04:46.84" becomes "09:04:46.84") -
set dtstr=%dtstr: =0%
set dtstr_long=%date:~6,4%-%date:~3,2%-%date:~0,2%T%time: =0%
echo %date% %time%
echo %dtstr%
echo %dtstr2%
echo %dtstr_long%
C:\>echo %date% %time%
14/05/2018 15:02:01.27
C:\>echo %dtstr%
20180514T150201
C:\>echo %dtstr2%
2018-05-14_15-02-01
C:\>echo %dtstr_long%
2018-05-14T15:02:01.27
C:\>
How to lock desktop cmd 0parating system