old school: use xcopy to back up your stuff

Need an easy way to back up your files, but don’t feel like monkeying with expensive or complicated software? Using the xcopy command and a simple text file, you can accomplish this rather easily. Here’s what I do.

First, make sure that file extensions are not hidden by going to My Computer -> Tools -> Folder Options -> View. Uncheck Hide Extensions for Known File Types and click OK (if it’s already unchecked, don’t worry about it).

Now create a blank text document on your desktop. Rename it to backup.bat. Now right click on the newly created file and click Edit. Do not just double click, because that will actually run the batch file—we want to edit it for now.

The syntax for xcopy is pretty simple:

xcopy “[source location]” “[destination location]” /switch

And then there are a bunch of switches you can use. I’ll give you an entry from my batch file, and then I’ll explain what the switches all do. To copy my files from my hard disk to my USB drive, I use (follow the syntax exactly, including the quotes!):

xcopy “C:\Docs\*.*” “G:\ANDYB\Docs” /d /s /e /c /i /h /r /k /o /y /f

As you can see, wild card characters can be used to filter your backup. I just use *.* to get everything. Now, the switches:

  • /d
    Copies only source files that are newer than existing destination files. Basically, existing source files that have not been modified since the last backup will not be copied, saving time (if you’re like me and have 50GB of MP3s, this is an essential switch!)
  • /s
    Copies directories and subdirectories. This is a very important switch, as most of us will have stuff in subdirectories we want to back up.
  • /e
    Copies empty subdirectories. I use this, although I probably don’t really need to.
  • /c
    Ignores errors. This alone is the reason I love xcopy: one corrupted file can bring XP’s regular file copy to a screeching hault. But with xcopy and this switch enabled, corrupt files are skipped and your backup continues (FYI: the regular Vista copy is smart like xcopy, and will do the same thing).
  • /i
    If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes destination specifies a directory name and creates a new directory. Then, xcopy copies all specified files into the new directory. By default, xcopy prompts you to specify whether Destination is a file or a directory (directly from MS…I don’t fully get this one, but it works better with it enabled).
  • /h
    Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files.
  • /r
    Copies read-only files.
  • /k
    Copies files and retains the read-only attribute on destination files if present on the source files. By default, xcopy removes the read-only attribute.
  • /o
    Copies file ownership and discretionary access control list (DACL) information.
  • /y
    Suppresses prompts (overwrites existing files automatically). Unless you want to babysit your backup, you’ll want to enable this option for sure.
  • /f
    This shows the full source & destination paths of each file being copied.

There are other switches, like /exclude, which—surprise—allows you to exclude one or more files and/or directories. Finally, to run your batch file, simply double click it. Again, remember if you want to edit it, you right-click and choose Edit.

<Nerd Alert>
If you want to be really nerdy, you can have xcopy output everything to a log. I guess if you want an inventory of everything that has been backed up, this might be useful to you. Just add > [drive]:\log_file_name.txt to the end of each xcopy entry. For example:

xcopy “C:\Docs\*.*” “G:\ANDYB\Docs” /d /s /e /c > c:\backuplog.log

To append an existing log, use >> [drive]:\log_file_name.txt, like this:

xcopy “C:\Docs\*.*” “G:\ANDYB\Docs” /d /s /e /c >> c:\backuplog.log

</Nerd Alert>

Now if you want to get really lazy, you can set your batch file to run as a scheduled task. Most people know how to use the task scheduler in Windows, so I won’t go into that for now…unless someone demands it! :)

P.S. If you want, you can download xcopy here, but Win 2K and XP users should have it by default. I’m not sure if it runs under Vista or not. Try at your own risk!

Andrew Block

I (Andrew Block) am the Director of Analytics and Insights for MSP-C, an awesome brand journalism company in Minneapolis, MN. Insights are everywhere - and this blog explores that premise. One part geek and one part analyst, I like to blog about techy and insightful things. Or insightful techy things...or....

Andrew Block wrote 117 posts

Post navigation

  • Walt

    Check out xxcopy — that’s with TWO x’s. That’s a software product from Pixielabs but the free for personal use version will do very spiffy backups along the lines being described here. In particular:

    xxcopy C:\ F:\ /clone

    would copy everything on the C drive to the F drive, get rid of anything on F that’s not now on C, goes all the way down the folder chain, including hidden files and so on, and does an incremental copy — if the same exact file is already there, nothing is copied.

    There jillions of other switches in case (for example) you want to LEAVE files that aren’t on C anymore on F in case you want to recover them. Yes, it can be run as a batch command — the documentation is EXCELLENT.

    xxcopy is a fine tool for making a clone in order to replace your hard drive with a new drive; the detailed directions are in xxcopy technical bulletin #10:

    http://www.xxcopy.com/xxcopy10.htm

    I’ve just cloned a new ’98SE disk.

  • http://www.marketingautomator.com Joe

    @Carl

    “Is there a way to get XCOPY to delete those?”

    No, but you can use robocopy.exe instead of xcopy.exe

    Robocopy supports a /purge and /mir switch.

    /purge will delete files from the target directory that are not present in the source directory.

    /mir does the same as /purge except you don’t need to add the /e switch to make it recursive.

  • http://andrewblock.net Andrew Block

    Thanks for the feedback, jack. :)

  • jack

    thank you very much.. got exactly what i want :)

  • http://twitter.com/Carl242 Carl Spanoghe (@Carl242)

    Cool. What about files that you have deleted? Is there a way to get XCOPY to delete those? Otherwise, when I (hopefully never need to) retrieve my files from my backup, there will be a lot of obsolete stuff there.

  • Dennis

    > f:\xcopy.log does not work in Windows 7 task scheduler. It won’t even run with that in there.

  • Pingback: Old school: Use xcopy to back up your stuff | Andrés Jaimes

  • http://andrewblock.net Andrew Block

    Michael,

    I don’t think you need the * on the destination drive. Also, this should work, but remember you’re just copying files and not cloning the drive. If you want to clone the drive, I’d suggest a program Like Acronis True Image…or I think Windows 7 has a built-in backup utility, but I have not used it.

  • Michael

    If I just have this exact text on the .bat file:

    xcopy”C:\*.*” “G:\” /d/s/e/c/l/h/r/k/o/y/f

    will this make a copy of the entire drive straight to the G: drive? And do there need to be any spaces in the middle like I have currently?

  • clevetbs

    dir /s/b C:\Users\%USERNAME%\Dropbox\*%Working%* > C:\Users\%USERNAME%\output.txt

    xcopy C:\Users\%USERNAME%\Documents\*.xls < C:\Users\%USERNAME%\output.txt /e /c /y /D /i

    this is one with varibles. i made this one but it doesnt put it into the folder. Help

  • http://andrewblock.net Andrew Block

    @Stephen Lang,

    Sweet. Good to see people are still using good ol’ XCopy. Lol.

  • http://www.93ft.com/ Stephen Lang

    +1 Just what I was looking for :)

  • Tim

    hey bob do this put a space betweenthe first” and the second ” and that will fix it

  • Dan

    Try putting spaces between your switches. (not sure if this would help)

  • Bob

    Here is what I am using. Maybe you can tell me why I get the error.

    Xcopy “D:\My Pictures\*.*” “F:\My Pictures” /D/E/C/R/S/H/I/K/Y

    This gives me Invalid number of parameters.

  • Owen

    Thanks Old School Andrew,

    And here i thought i had to learn the ins and outs of scripting to use command lines.

    Owen

  • Henry

    Thanks – this breaks it down for me so easy.

  • http://andrewblock.net Andrew Block

    @Robert,

    No problem–glad it helped you!

  • Robert Kirby

    Thanks, just what I hqve been looking for.

  • http://andrewblock.net Andrew Block

    @Hans,

    Cool, glad you found my little xcopy tutorial useful. Do also check out Robocopy, especially if you’re a Vista user.

    http://www.mydigitallife.info/2007/05/07/robocopy-syntax-command-line-switches-and-examples/

  • Hans Arneberg

    Thank you, this was just what i wanted.

    Hans

  • http://andrewblock.net Andrew Block

    Good call. Robocopy is probably a better, more updated tool. I have just been too lazy to learn the syntax. :(

  • Gup

    Also check out RoboCopy:
    http://en.wikipedia.org/wiki/Robocopy