Help on a batch file

hi there,

I would like to have a batch file or VB script that will do full and incremental copy of My network drive Z to E (local drive). The Z drive have multiple folders and sub folders. I will create a Job task on the computer to run the full backup once every 2weeks and the incremental will run daily.

I would like copy to overwrite the last week copy to avoid running ot of space n the local drive.

Thanks, Habib
Habib ZakariaNetwork Solutions ArchitectAsked:
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.

I believe the Xcopy command will be of use for you.  It's an extremely powerful copy tool for MS-DOS.  I'm including a link to all the flags needed.
Michael OrtegaSales & Systems EngineerCommented:
How are you then going to audit backup success/failure of that data? I would recommend looking at 3rd party applications to do your backups.

A couple mainstream names would be Symantec BackupExec and Acronis. There are several flavors of backup applications so you'd want to choose the one that best matches your environment.

Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
No I really do not care about the audit. I just want the job run.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
I agree X copy is the commad but the switches are confusing to me
What switch do you need help with?
Steve KnightIT ConsultancyCommented:
Hav you thought of just using the built in ntbackup.exe?  You can schedule it to run a full or incremental backup daily, weekly, each friday, second friday of the month etc.... it's not the flashest backup program in the world, but it does do the job.  You end up with all the files in one large ".bkf" file which you use ntbackup to restore from.

If you do want files then if you want a full and incrementals then try something like:

full backup:
xcopy C:\sourcedir\*.* d:\destdir /s /e /h /r /q /o /y

For your incrementals at a simple level you could run them to a folder each, e.g.

xcopy C:\sourcedir\*.* d:\destdir\day1 /s /e /h /r /q /o /y /m

Th /m on the end backs up only files that have been changed (or at least those that got marked for archive.. which SHOULD be the same). It then clears the archive bit so that file doesn't get backed up again until it is modified again.

You can wrap the copy.. or use robocopy which is more powerful in a batch file and start setting directory names for destination based on the date perhaps, or keep so many old backups.  lots of possibilities, depens what you want to do.

e.g. here is one script of mine that does a simple backup of some files and keeps so many of the backup files:

You can have it send you an email perhaps if it fails:

You can get a simple date for using as a foldername using code such as this:

set TODAYDATE=%date:~7,2%-%date:~4,2%-%date:~10,4%
if "%date:~2,1%"=="/" set TODAYDATE=%date:~3,2%-%date:~0,2%-%date:~6,4%

See my article here for more ways with date based folders etc.

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

You should have a serious look at ROBOCOPY - much more suited to such challenges than XCOPY. It has been made by Microsoft and you find more infomation here:


   Windows front-ends (GUIs) for ROBOCOPY:

   Microsoft TechNet - ROBOCOPY GUI:


     "This simple tool was written by a Microsoft engineer named Derk Benisch, and all it really did was
     create a graphical interface for the very popular Robocopy command-line utility, which provided
     file copying capabilities far beyond what was built into Windows" - TechNet Magazine

Hello Habib

You have received some excellent suggestions.  Let me just add one other option you may wish to consider.  I use Karen Kenworthy's free "Replicator" program:

You can use it in a few different ways after creating your different "jobs" in the program interface:

1. Allow it to run as a startup program, minimized and using its own scheduler
2. Stop the timer and Exit the program, then later call it from the command line using the "job" name as the parameter.

Creating the separate "jobs" is easy enough and the program's help file explains all the options well enough for you to decide which settings to choose.  One setting is very important to choose carefully though, and that is the "Replicate Folder and File Deletions?" setting.  IF you enable this, the program keeps the source and destinations' contents synchronised, and will DELETE contents in the DESTINATION IF they have been deleted from the SOURCE since the last backup!  DO NOT tick this box for your purposes.  You are not syncing folders or drives.

The default behaviour of this program is to temporarily rename the file in the Destination Folder BEFORE a new copy is created there.  If the new copy is made successfully, the old copy is then deleted.  However, if the copy process fails for any reason, the old file will be restored by giving it back its original name.  This program default is a good safety buffer, but can use up extra space while processing.

There is, however, a job setting that overrides this default behaviour whereby the existing contents in the Destination are deleted BEFORE the copy begins.  The setting is (intuitively) entitled : "Delete Old Copy Before Creating New".  It is a potentially dangerous option to enable.  If hard drive space should become an issue enough that you would consider enabling this option, then you need a larger capacity destination drive rather than compromising like this.

Instead of enabling the "Delete Old Copy Before Creating New" setting for your weekly full backup, a better option would be to RENAME the existing folder in the destination and then delete it IF the Replicator program ran the job successfully.  This could be achieved by simple batch commands like testing the program's exit code and perhaps comparing the source and destination folders.

Apart from the above two warnings, the other settings are fine for doing an incremental update to the destination OR a full replacement in the destination.

When you create a named "job" the name and settings for that job are written to or appended to the file (on Windows XP):

"C:\Documents and Settings\<YourUserName>\Local Settings\Application Data\Karen's Power Tools\Replicator\Jobs.txt"

As long as you know the name of each job you can call them like this from the command line:

"C:\Program Files\Karen's Power Tools\Replicator\PTReplicator.exe" "Name_Of_Job"

If you were executing one job after another from a batch file it may be necessary to launch "PTReplicator.exe" with the START command like this:

start /wait "C:\Program Files\Karen's Power Tools\Replicator\PTReplicator.exe" "Name_Of_Job"

Otherwise you can just set the schedule for each job within the Replicator program and leave the program to autostart with Windows minimized to the System Tray awaiting the call.

The Windows Task Scheduler should accept the command lines for separate tasks and you wouldn't need a batch file or the Start /wait command.  That may be the safer option (ie. a Windows Service) than relying on a standard program startup.

You might as well test out the program to see if it does what you need, but I suggest using it on test folders/drives first.  It is only free for NON-commercial purposes, but it's only $25 a seat for 3 to 9 seats if used commercially.

Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
Excellent suggestions. Thank you.
Thank you Habib
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.