Auotmatically generating a folder name with robocopy

I am using robocopy via a one line batch file and Task Scheduler to backup a folder on one server to a removable hard disk that is on a different server. The disk is large, my backup quite small and i often need to refer back to previous days so don't see the harm in creating a new folder each time rather than overwrite the exsiting one.

Ideally I would like to auto generate a folder on the removavble disk based on today's date and then have robocopy backup to this.

Can aybody let me know whether there are any switches in robocopy that will let me do this or whether there is something else i can add to my batch file to do it?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Rob StoneCommented:
Add the following to the batch file:

set dd=%date:~0,2%
set mm=%date:~3,2%
set yy=%date:~6,4%

md %yy%%mm%%dd%
cd %yy%%mm%%dd%
robocopy .....

You may have to tweak it a little, but it's a general overview on doing it in a batch file.

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
metamaticAuthor Commented:
Nice one, tweaked it a bit and it works fine.

However, just got a one final query....

The backup runs at 6am but i would like it to be dated the previous day. Can you let me know how I can roll it back a day. (i assume i have to insert a "-1" in there somewhere!)
Doing math with dates is a major pain in batch.  Here's a way to do it by creating a temporary vbscript.

@echo off
set script=%temp%\tempscript.vbs
echo WScript.Echo Year(Date - 1) ^& Right("0" ^& Month(Date - 1), 2) ^& Right("0" ^& Day(Date - 1), 2) > "%script%"
for /f %%G in ('cscript "%script%"') do set stamp=%%G
del /f "%script%"
echo %stamp%

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Totally agree that doing math in BAT is a pain.  VBS is a good way to approach it, or there are a few very small free utilities that can be leveraged as well.  One I keep handy can be found at:

and allows a command like this to be executed:

doff mm-dd-yyyy -1

this will return 04-19-2010

In a BAT script it's easy to capture this output and place it in a batch variable, which can then be used in the name of a file, etc.  Here's a small sample:

for /f "tokens*" %%A in ('doff mm-dd-yyyy -1') do set Yesterday=%%A
copy file.txt file-%Yesterday%.txt

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
Windows Batch

From novice to tech pro — start learning today.