Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a batch process that dumps data from my database (windows 2008 r2) and I'd like to dynamically name my file with the day of the week included.

is that possible to do with a simple .bat file?

if so how?

share|improve this question
When you say 'day of week' you want a format to look like: "20111019-Wednesday"? – RobW Oct 20 '11 at 0:19
up vote 0 down vote accepted


Assumes you have your regional date setting as such:

  • Short: M/d/yyyy
  • Long: dddd, MMMM dd, yyyy

At the top of your batch file, you can set an environment variable called fname:

set fname=%date:~0,3%

Today (which is Wednesday) fname would be set to Wed. In your batch file, you would replace the static file name with %fname%

You can add static text to this dynamically changing name easily:

set fname=%date:~0,

fname would be set to:

As I commented above, you can add more date related info:

set fname=%date:~0,3%-%date:~10,4%%date:~4,2%%date:~7,

fname would be set to:

You can add a path by either prepending to your 'set fname' variable, or when you use the %fname% variable in the script: set fname=c:\backup\%date:~0,3%-%date:~10,4%%date:~4,2%%date:~7,, or at the point of use: "c:\backup\%fname%"

Remember to use Quotes if there are spaces in the path or filename.

share|improve this answer
this looks great. How would I "inject" the file name into the path where I"m saving it (into that whole command)? Ex: "c:\backup\"+fname? – Christopher Johnson Oct 20 '11 at 15:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.