≡ Menu

Create unique named file from batch script

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.

To create a unique file with just date

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

To create unique file with date and time

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

Creating directory with unique name using 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, printing all directories in the folder shows the newly created folder with date in the name

c:\> dir /A:D /b
11-15-2015
4 comments… add one
  • Pete Wood

    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.

    • Srini

      That’s a good idea. Thanks Wood for sharing it.

  • Michael

    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

  • AJS

    > 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:\>

Cancel reply

Leave a Comment