Batch Get current date as filename

If you are working with tools that genegates logfiles it is handy to have the name of the logfile based on the date. The following command will help us create a filename based on the system date. The command takes the date, puts it in MMDDYYYY format and appends the .LOG extension. The date with the .LOG extension is assigned to a variable named datefile. For example, today, on July 27, 2007 the datefile variable would contain: 07272007.LOG

The command for setting up the datefile variable is:

for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set datefile=%%i%%j%%k.log

If you want to view the contents of the datefile variable, you can do so by using the echo command. Since datefile is a variable, you must place percentage signs on each end of it any time you reference it. For example, the command for viewing the contents of this variable would be:

Echo %datefile%

But we’re not interested in merely viewing the contents of the datefile variable. Our goal is to use the variable’s contents as a filename, and to export eg. the CHKDSK results to the file that is referenced by the datefile variable. To do so, use this command:

Chkdsk > %datefile%

The batch file in its entirety would look like this:

for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set datefile=%%i%%j%%k.log Chkdsk > %datefile%