Batch Files : Date Stamp in a filename

Ive been asked this a number of times this past month....  How do I get the date into a filename in a batch file?

Most people try using the %date% variable.  Which will not work as you cannot have /'s in the filename.

So, try this:

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do set datestring=%%i%%j%%k

You can now use %datestring%.txt as the filename

Note: The delim character changes depending on your locale.  Eg. deliims=. for Germany.  You may want to change the filename to %%k%%j%%i to get multiple logs sorted in order within Windows Explorer sorted by YearMonthDate

Comments

  • Anonymous
    November 04, 2014
    Batch Files : Date Stamp in a filename - PFE Ireland - Site Home - TechNet Blogs