Thursday, January 31, 2008

Dates in Batch Files

I was using a batch file to kick off a process that grabs a file off a server. One of the arguments of the command to get the file tells the command where to store the file, including the file's name. We wanted the file's name to based of the date the file was stored on our servers. So I needed away to get the current date and passed it in as a command line argument. Here's what I found:

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

MyCommand.exe FileName="MyFile%Year%%Month%%Day%.txt"

This command works very well.

Source: http://weblogs.asp.net/whaggard/archive/2005/08/18/423029.aspx