≡ Menu

Backup files using Xcopy in command line

There are lots of commercial data back up solutions available in the market. If you are a home user or a small business user of Windows OS and want to backup all your important data then this article shows you a simple incremental backup solution with no additional cost. If you can’t afford to buy a commercial backup application don’t worry! You can still backup your data using Windows xcopy command.

Windows incremental backup

If you already understand what incremental backup is and it’s advantages, you can skip this and move on to next section that explains the syntax.

You can use a USB drive or an external hard disk as the backup location. Copying files to another partition on the same is okay but it may not serve the purpose as a disk crash will cause data loss on all the partitions of that disk. Let’s say your data is in the folder D:\Data and you need to backup your data to Z:\Backup folder (Z: is mapped to some external disk storage device). We can just copy data from D:\Data to Z:\Backup directly using any copy command or tool but it will involve copying the files even though they are not modified from the time they were copied previously. I will explain this in detail. Let’s say you take data back up every week. The first time you back up the data you need to copy all the files in your disk to the backup location. In the second week, you need to copy only the files that have been changed during the last 7 days. You do not need to copy the files that have not been touched since the time you took the last back up. This will save time and also decreases CPU usage and will also lead to increased lifetime for the storage devices. So a backup utility should be able to detect which files are modified and which are not. This is called incremental back up.

Using Xcopy command for incremental backup on Windows

Now let’s see how we can do this using Windows Xcopy command. Xcopy has different switches using which control what files to be copied. We will use these switches to make Xcopy work as a backup utility.

You can run the below command to back up data.

xcopy  source  destination  /M /E /G /H /Y

This one uses archive attributes of files to distinguish the files that have not been changed since the time last back up was taken. /M switch turns off the archive attribute on the files after copying the file to the destination. This attribute will be set only if someone modifies the files after wards.  For the above example the command is given below.

Xcopy  D:\Data  Z:\Backup  /M /E /G /H /Y

Back up data using date and time stamps

We can do incremental backup by specifying the date the last back up was taken. Xcopy will copy only the files that have changed after this date. For this we need to run the below command.

xcopy source destination /E /G /H /Y /D:mm-dd-yy

Hope this post has helped you. Let me know your comments on this.

15 comments… add one
  • Muthu

    How to automate the backup operation? If I want to run backup using xcopy every Monday, how can I do it.

    • nerf

      You can easily automate operations using Windows “Scheduled Tasks”.
      Simply craft your commands in Notepad, save it as a batch file (name.bat), then create a scheduled task that executes the batch file. You’ll find the Scheduled Tasks Manager under Start>Accessories>System Tools.
      It’s pretty intuitive- creating a new task is much like creating a new event in a calendar, except that ya can brows to where you saved the batch file.
      need more?
      jfgi ;^)

  • Nanaiah

    How to take backup file if the the folder name has dot in it.
    For example c.files.com

    • admin

      Did you have any issue using xcopy on a folder with dots in the name? I just tried xcopy on such a folder and it worked.

      C:\>xcopy folder.with.dots.source folder.with.dots.dest /E /G /H /Y
      folder.with.dots\1.docx
      folder.with.dots\1.pdf
      2 File(s) copied
      C:\>dir folder.with.dots.dest
       Directory of C:\folder.with.dots.dest
      
      08/02/2011  07:37 PM           451,071 1.docx
      06/16/2011  05:06 PM           387,290 1.pdf
        
    • I need to copy a folder & contents that has a dot(.) in the name to another sub-folder.
      I can make it work if I include the explicit folder name (such as 1234test.NCD), but not with a wildcard (*.NCD) . In other words to put a copy in the MAKE\EXAMPLE subfolder:
      a) xcopy /i /y /q 1234test.NCD MAKE\EXAMPLE\1234test.NCD works.
      b) xcopy /i /y /q *.NCD MAKE\EXAMPLE\*.* does not work – no error messages.

      Any ideas?

  • jim

    You might also mention / discuss xcopy’s problems with file / filepath size, and it (sometimes?) turns the destination directory into a hidden directory (requiring some attrib intervention to find again). Have a great weekend.

  • slyaii

    “You might also mention / discuss xcopy’s problems with file / filepath size, and it (sometimes?) turns the destination directory into a hidden directory (requiring some attrib intervention to find again). Have a great weekend.”

    You can fix that by adding *.* to the source, that will fix the hidden directory.
    example,
    xcopy SourceFolderOrFile \*.* DestinationFolderOrFile

    If you do have hidden directory, in windows 7. Tools >> Folder Options >> View Tab >> Uncheck “Hide Protected Operating System Files (Recommended)”

  • Steve Kohn

    Very useful. I’m troubleshooting right now, and think I’m using the wrong switches. Eager to try yours.

    My xcopy backup starts, copies the first six or so folders in the source directory, then stops at the same place every time. Not because the destination drive is full…it’s a TB drive, mostly empty.

    Am off to change the switches.

  • Rene

    Hi, I did the backup following this method, but now that I’ve restored my pc to its original settings, how do I restore my backup so I can re-install the software that I have in my backup file? Any help would be greatly appreciated.

  • TMLMANAGER

    Hay SRINI
    i must say thanks a lot for your great and super cheap solution
    It works!

  • Omar

    Anyone know how to use xcopy so that it only backs up files that are going to be moved. For example say we have bunch of files in DEV environment that we’d like to copy and overwrite onto QA environment (and new files in different locations within main app folder) – but before we do we’d like to back up the same files that currently exist in QA onto a backup folder.

  • charles

    Please if l want to do an incremental backup after copying the whole files for the first time,does it mean l will need to modify the batch file with the /D:mm-dd-yy which was not in the script when l first ran the batch file to command everything

  • Voyteck

    Hi,
    Strange thing. I prepared batch file (for scheduling) and noticed that the script did not copy all files. Then I copied the command from script, run it and it worked fine. Anybody knows where is the problem? Thank you in advance for help :)

  • Brad Mathews

    I recommend adding the /C switch (ignore errors) if you are going to run this in a batch file. I also am putting a @pause at the end of the bat file so that the cmd windows stays open and I can see it was run each morning.

  • Nev

    I have got a sharing violation error.

Leave a Comment