Welcome to TechNet Blogs Sign in | Join | Help

PFE Ireland

Welcome to the Irish Premier Field Engineering blog
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

Posted: Thursday, May 08, 2008 5:35 PM by gmcshera
Filed under:

Comments

No Comments

Anonymous comments are disabled
Page view tracker