Dos - Use timestamp as unique filename

By xngo on September 14, 2019

In DOS, you can use %date% and %time% environment variables to create a relatively unique string. I usually use this for my filename. These two environment variables will return date and time according to your locale. So, you may have to change the code below depending on your locale.

SET Unique=%date%_%time%
SET Unique=%Unique:/=.%
SET Unique=%Unique::=.%
SET Unique=%Unique: =%
SET Unique=%Unique:Mon=%
SET Unique=%Unique:Tue=%
SET Unique=%Unique:Wed=%
SET Unique=%Unique:Thu=%
SET Unique=%Unique:Fri=%
SET Unique=%Unique:Sat=%
SET Unique=%Unique:Sun=%
 
echo %Unique%
 
SET Unique=&::

Output

>SET Unique=Fri 09/12/2019_19:22:12.10
 
>SET Unique=Fri 09.12.2019_19:22:12.10
 
>SET Unique=Fri 09.12.2019_19.22.12.10
 
>SET Unique=Fri09.12.2019_19.22.12.10
 
>SET Unique=Fri09.12.2019_19.22.12.10
 
>SET Unique=Fri09.12.2019_19.22.12.10
 
>SET Unique=Fri09.12.2019_19.22.12.10
 
>SET Unique=Fri09.12.2019_19.22.12.10
 
>SET Unique=09.12.2019_19.22.12.10
 
>SET Unique=09.12.2019_19.22.12.10
 
>SET Unique=09.12.2019_19.22.12.10
 
>echo 09.12.2019_19.22.12.10
09.12.2019_19.22.12.10

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.