Search code examples
datebatch-filecmdformat

Format date in Windows 7 batch job


I'm trying to use the current date in a Windows 7 batch job. The batch job opens multiple files which have today's date appended to them. Example:

start \\\Directory_Name\Rpts\20130801\0000A060_FileName_20130801.pdf

start \\\Directory_Name\Rpts\20130801\0000P083_FileName_20130801.pdf

start \\\Directory_Name\Rpts\20130801\00007P12_FileName_20130801.pdf

If I run echo %date% I get: "Thu 08/01/2013"

I know I can run echo %date:/=% and get: "Thu 08012013*"

But I want to remove the "Thu" (today's day) and format the date to "20130801" (yyyymmdd) instead of mmddyyyy.

So eventually the open file command would look like the following with the correct %date% command inserted: start \\\Directory_Name\Rpts\%date%\00007P12_FileName_%date%.pdf

Anyone know how I can do this?


Solution

  • This is a bit simpler of a way of doing it with substrings:

    set buildDate=%DATE:~4,10%
    set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%