Backup/Copy Files that are 'In Use' or 'Locked' in Windows (Command Line)

If you’ve ever tried to copy a file that is locked by another application, you’ve probably seen an error message similar to “The process cannot access the file because another process has locked a portion of the file”. So how do you copy it anyway?

backupcopy-files-that-are--and-quot;in-use-and-quot;-or--and-quot;locked-and-quot;-in-windows-command-line photo 1

Since XP, Windows has supported a technology called Volume Shadow Copy, which is used to power the previous versions feature in Vista as well as System Restore and backups. What it does is take a temporary snapshot of the file or drive, and then allow an application to read from the snapshot even while other applications are accessing or modifying the file.

backupcopy-files-that-are--and-quot;in-use-and-quot;-or--and-quot;locked-and-quot;-in-windows-command-line photo 2

What we can do is use a command line utility called HoboCopy that utilizes this service to copy the file.

Understanding the Prerequisites

HoboCopy and most other backup utilities make use of two services in Windows, and you’ll need to verify that these services are not disabled:

  • Volume Shadow Copy
  • Microsoft Software Shadow Copy Provider

They can be left as Manual startup, so they don’t need to be running all the time. Hobocopy will start the two services automatically when needed, and the Volume Shadow Copy service will be turned back off after it’s done.

Using HoboCopy to Backup/Copy a Single File

The syntax is a little weird, because HoboCopy is really meant to be used for backing up an entire set of folders. We can use it to backup a single file by passing in the filename argument at the end.

Note: on Windows Vista you will need to launch an Administrator mode command prompt by right-clicking on the Command prompt in the start menu and choosing Run as Administrator.

Syntax:

hobocopy c:\directoryname\ d:\backupdirectory\

For example, I want to backup my c:\users\geek\mail\outlook.pst file to d:\backups\outlook.pst. Here’s the syntax that I’d use:

C:\> hobocopy c:\users\geek\mail\ d:\backups\ Outlook.pst

HoboCopy (c) 2006 Wangdera Corporation. hobocopy@wangdera.com

Starting a full copy from c:\users\geek\mail to d:\backups\
Copied directory
Backup successfully completed.
Backup started at 2008-03-09 01:57:28, completed at 2008-03-09 01:58:39.
1 files (606.45 MB, 1 directories) copied, 7 files skipped

Using HoboCopy to Backup an Entire Directory

A much more useful task would be to backup my entire User folder, probably to an external hard drive for safekeeping. For this, we’ll want to add a couple of command-line arguments.

/full Copy all files /skipdenied Ignore any access denied messages because of permission errors. /r Copy recursively /y Don’t prompt, just copy everything

Syntax:

hobocopy /full /skipdenied /y /r c:\directoryname\ d:\backupdirectory\

Let’s go with the same example, I want to backup my entire user directory to d:\backups\, so I’d use this command:

hobocopy /full /skipdenied /y /r c:\users\geek\ d:\backups\

This command will likely take a very long time to complete, so you might want to take a nap or something. At the end you should have a nearly perfect copy of the directory… if there are any permission errors you’ll be alerted to files that didn’t copy. Realistically any files in your user directory shouldn’t have this problem.

Using HoboCopy to Incrementally Backup a Drive

Hobocopy also supports backing up files incrementally, so it will only copy the files that have changed since the last backup. This works similarly to utilities like rsync, except hobocopy stores the last backup date in a file that you need to specify on the command line.

/statefile=filename This flag specifies the file that contains the last backup information. /incremental Only copy files that have changed since the last full copy.

Syntax:

hobocopy /incremental /statefile=filename /y /r c:\directoryname\ d:\backupdirectory\

Example:

hobocopy /incremental /statefile=d:\lastbackup.dat /y /r c:\users\geek\ d:\backups\

The first time that you run this command you will need to use /full instead of /incremental, or else you will get an error because the state file hasn’t been created yet. After that you can run the incremental backup with the /incremental switch.

This would be an excellent way to automatically backup a set of folders as part of a scheduled task.

Download HoboCopy from sourceforge.net

Article Backup/Copy Files that are 'In Use' or 'Locked' in Windows (Command Line) compiled by Original article here

More stories

Get Tabs for your PuTTY

It seems like everybody uses the free PuTTY client for accessing SSH from Windows, but the lack of a tabbed interface has kept me using the commercial SecureCRT application for years… until now.

Make Backspace Key Work Correctly in Firefox on Linux

If there’s one thing that drives me crazy about using multiple operating systems, it’s the inconsistency in keyboard shortcuts… when you hit the backspace key in Firefox on Windows it normally goes back to the previous page, but it doesn’t on Ubuntu Linux.

How to Disable SuperFetch on Windows Vista

The SuperFetch service in Windows Vista preloads your system’s memory with the applications that you use most often. This makes launching of those applications much faster, but it might be an unwanted behavior for system tweakers or gamers.

Make Menus Load Faster In XP

I am not a fan of the delay that can occur between clicking the start menu and waiting for it to display.  Here is a quick Registry tweak that will allow for the start menu to display more quickly

Change or Set the Greasemonkey Script Editor in Firefox

If you are a fan of the Greasemonkey extension for Firefox you might wonder how to change the script editor/viewer as there is no option for this in the UI. It’s even worse when you download scripts from some website and the line endings don’t work in the inferior default editor.

Ron Paul Toolbar Installation

For anyone even semi-familiar with the Internet I am sure you are aware of the Ron Paul Revolution.  His name currently is the #1 googled subject right now.  The Ron Paul presidential campaign has basically taken over YouTube and his support has gained reputable positions on popular social