How to Create Unique Named File From Batch Script? You can easily create files with unique names from the batch files by windows command prompt. So, let’s see how can we implement this in a Windows batch file.
Read Also: How to Start/Stop Service from PowerShell
How to create a unique file with just date –
If you created the file just once per day and it’s very helpful.
Just you need to run the 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%
When you running the above command and a new file gets created today’s date.
c:\>dir 11/15/2015 07:35 PM 2,288 11-15-2015.txt
How to create a unique file with date and time –
If you want to run the script the multiple times in a day, you need the date would not be enough. Just adding the timestamp to the file name and finally ensures 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%
Read Also: How to Install PowerShell Help Files
When above running the batch file, you will see the new file created with date and time.
c:\>dir 11/15/2015 07:44 PM 2,487 11-15-2015-19-44-38.48.txt
Just creating the directory with a unique name using the date.
@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 and printing all directories folder shows the newly created folder with the date in the name.
c:\> dir /A:D /b 11-15-2015
Read Also: How to Reboot Windows from PowerShell