Population: 9,142 Search:

The Stuffopolis.net Multiview Forum
Topic Tags: ExternalHardDrives, InternalHardDrives, Software, WindowsSoftware

Profile | Register | Search

The Multiview Forum Views: 5841   Post Reply 
Article/Guide: Backup Only if New or Modified with a Batch File

 Posted - 5/19/2009 12:14:49 AM  - Show Profile  Edit Message

Site Administrator
2017 Contributions
Article Title: Backup Only if New or Modified Batch File
Category Resource Page: InternalHardDrives

There are many cases when I want to back up a certain directory and keep it synchronized with my current copy on my main hard drive - such as Pictures, My Documents, etc.. I don't want to make an entirely new copy every time since that would take too long and puts extra strain on the drive. In some cases when the reason for the backup is because the hard drive is failing, just doing a 'Replace All' copy may also overwrite a good uncorrupted copy with a corrupted one.

I've been thinking about finding an application or Windows XP extension to do this sort of thing - but I've never gotten around to it. Now I have, and doing some searching around I found a very useful Batch file that does everything I want quite well and does it lightning fast.

Here is the exact code as it appeared on the original site (reference link below):

@echo off
:: variables
set drive=G:\Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"

echo ### Backing up Favorites...
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"

echo ### Backing up email and address book (Outlook Express)...
%backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book"
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express"

echo ### Backing up email and contacts (MS Outlook)...
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook"

echo ### Backing up the Registry...
if not exist "%drive%\Registry" mkdir "%drive%\Registry"
if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg"
regedit /e "%drive%\Registry\regbackup.reg"

:: use below syntax to backup other directories...
:: %backupcmd% "...source directory..." "%drive%\...destination dir..."

echo Backup Complete!

"The above example backs up "My Documents", Favorites, Outlook Express email/address book, (all for the current user) and the Windows Registry. It copies the files to the directory defined in the %drive% variable, or "G:\Backup". If the script is ran multiple times, it will only rewrite if the source files are newer. It will create subdirectories as necessary, and it will retain file attributes. It can copy system and hidden files."


Use that as a base to work from, and give a test with one of the smaller directories to make sure it does what you want. For me though, it was perfect - I just added a bunch of other folders, changed the destination directory and that was it. I also made a custom copy to backup just about everything off one of my failing drives as well - have to say this little script has saved me hours .


Last Edited By - PJPeter on 12/18/2009 3:02:38 PM

 Posted - 5/19/2009 12:39:48 AM  - Show Profile  Edit Message

Site Administrator
2017 Contributions

One other note: If you get an "Insufficient Memory" error while running this or a modified batch file it is most likely because xcopy has a 256 character limit. If you are able to shorten the copy path, especially by changing some overlong direct names (eg. 'Backups January 19th 2009' -> 'Bkup Jan-19-09').

Note that you may need to do this on both ends, on the new backup location and on the original one, depending on whether you are actually copying the too long file name/directory path or if it is above the level you are working at on one or the other drives.

Also note that if you renamed files or folders at the source they may be copied twice since the comparison is between files/folders with identical names. In my case this is not a problem - but if you notice a large difference between the original folder and the backup this might be why. If this is a problem for you, you may want to run a duplicate checker as well - or you can make a fresh copy, confirm it and then work from that as your new Backup.

Remember it is always a good idea to have at least 1 backup of any file you want to keep and preferably 2 or 3. If possible having a secure backup in some other building incase of actual physical damage is also desirable (eg. fire, flooding, etc...).

The Stuffopolis.net Database and website, for example, are backed up regularly in about 6 different places and on 4 different mediums (online, flash drive, internal and external hard drive and CD/DVD copy), plus additional occasional backups in a couple of other places as well - all to ensure nothing of use is lost.


Last Edited By - PJPeter on 2/19/2011 7:19:03 PM

 Posted - 2/19/2011 7:27:58 PM  - Show Profile  Edit Message

Site Administrator
2017 Contributions

As well, you can use robocopy, which is more robust than xcopy and does not have that insufficient memory problem/char limit issue:

robocopy C:\ J:\G /e /XO /r:1 /w:2 /LOG:robocopylog.txt /XD "C:\Documents and Settings" "C:\Windows" "C:\Users" "C:\ProgramData"

That's the string I'm currently using to back up my C drive onto an external disk with a minimal path size to prevent pathing limits. I skip the Junction points on the drive, since Windows 7 uses those directories differently than others - and if you do not skip them (Users, ProgramData and Doc and Settings) you will end up with recursive loops that will eat up your disk (Application Data/Application Data/Application Data/... until unable to copy). Note that you'll want to backup anything in the Users directory or other excluded folders manually - especially Desktop, Favourites, etc..

The rest of the flags are:
/e to include empty directories
/XO don't overwrite if there is already a current or newer copy in the destination directory (already exists, so why waste resources copying again?)
/r:1 to retry once instead of a million times as is default (if it's a sys file in use, I probably don't need it anyway, I just want a basic backup and 1 retry is fine for me)
/w:2 Wait 2 seconds between retries
/LOG: specifying to log to a file so I can check for problems, missing files, etc... at my leisure rather than just having output to command line
/XD to exclude the following directories, not the " " is required for paths with spaces. You can also use * before hand if you wish to get all paths matching that string (*temp would not get any directories with the name temp in them, for example)

Also note, I was getting all sorts of blue screens while using Robocopy on my G51Jx and copying certain files, especially those to do with Quicktime - it turned out to be an issue with the ASUS Data Security Manager driver - uninstalling it (something I'd never used anyway) solved the problem. I'm currently refreshing my system with an entirely new Windows install and this one a clean one after a year, and so these commands have been quite useful to me. Note that this method is better than the Drive to Drive Data Copy with WD Lifeguard Tools because it is harder there to exclude those key Junction point directories and it will loop as badly as this does without those exclusions.

Be sure to verify your destination files are valid with a Scan Disk check first.

Otherwise, happy back ups


Last Edited By - PJPeter on 2/19/2011 7:32:17 PM

 Posted - 7/9/2012 12:27:37 AM  - Show Profile  Edit Message

Site Administrator
2017 Contributions

And I'm actually now doing the wipe on my system I mentioned in the post above a year and a half ago - busy times.

Here is the robocopy command I use now:

robocopy C:\ F:\010712\ /e /mir /R:1 /W:0 /np /XJD /log:robobackup_log.txt

The source in this case is "C:\" and the destination is "F:\010712\"

The /XJD prevents the looping that would otherwise occur by preventing the junctions from being copied. To save time, I changed it to retry only once and to not wait at all in between.

Selecting /mir causes it to not just copy newer files, but to mirror the original with the other drive. So if you deleted something in the source, it will be deleted in the destination also. You can use this or XO depending on your needs.

/np means it does not display progress.

In the end, I think this is the better string - since excluding the entire Users directory as I did above can lead to some very useful data being lost - but a combination of these two could be what you want.

Happy backups ,


Last Edited By - PJPeter on 7/9/2012 12:28:46 AM

 Posted - 8/26/2012 9:25:27 PM  - Show Profile  Edit Message

Site Administrator
2017 Contributions

Last Edited By - PJPeter on 8/26/2012 9:31:01 PM

 Posted - 8/26/2012 9:25:41 PM  - Show Profile  Edit Message

Site Administrator
2017 Contributions

Last Edited By - PJPeter on 8/26/2012 9:30:47 PM

 Posted - 8/26/2012 9:29:44 PM  - Show Profile  Edit Message

22 Contributions

Last Edited By - PJPeter on 8/26/2012 9:30:37 PM

Quick Reply

Not Registered? If So Click Here - It's Completely Free!


Your Username:
Your Password:
   Remember Your Username and Password.
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert Image Address Insert HyperlinkInsert Email Insert CodeInsert QuoteInsert List

 Include Your Profile Signature.
 Subscribe To This Thread.

rolex replica watches


Back to the Forum | Post Reply

  Bookmark and Share