Using A Batch File to Append The Date to a Filename

I am using Windows 2000 and need to create a batch file to append the date to a filename. The filename is "order.txt" so for example I need to make it "order20041029.txt" - the date format being YYYYMMDD.

Through another example on Experts Exchnage I manage to do the following:

ren d:\orders\order.txt order%date:~7,2%%date:~4,2%.csv

This however gives the result "order1029.txt". Can you a) let me know what I need to do to amend my line of code and b) explain what it means? I would like the answer urgently if possible.

Many thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


at a command prompt type:

echo %date%

Then access portions of this resulting string in your batch file like this:

%date:~10,4%  (this yields '2004' for me)

This grabs the substring starting at position 10 and grabs four characters
(first character is zero)

This is how I would do it here, but your %DATE% variable might be ordered differently if not in USA:

ren d:\orders\order.txt order%date:~10,4%%date:~4,2%%date:~7,2%.csv


rabelleAuthor Commented:

Many thanks for that - I've amended the order of the %dates%'s and it works fine. Can you explain what the different numbers mean though?

Basically, if you have this:

set variable=1234567890

and then you had: echo %variable:~0,2%
that would 'echo' (output):  12

That basically processes everything from 0 to 2.

Equally, if you put:
echo %variable:~3,5%

that would 'echo' (output): 45678

So, the first number means how many to SKIP (from left to right) and then the second number is how many characters (or integers in this case) to process from there on..

Does that make sense? :o)

Best of luck,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

And then you have more options, such as:

set variable=1234567890

set var=%variable:2=7%

that would replace '2' with '7', so if you then put:

echo %var%

you'd get:


That's basically all there is to it ;-)

By the way; I'm just informing you of this, I'm not trying to nick the points.. so, all the points to go to biran ;-)

Sorry! I mean Brian :)
rabelleAuthor Commented:
Thanks to both of you for that - apprecaited :o)

Oh. 8-) Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.