POSTS

Copying Locked Files via Shadow Copy

Microsoft Windows sometimes locks files even against reading which makes copying, or backups brittle and error prone. This can be especially annoying with cloud storage like Dropbox, pCloud, Google Drive, and Microsoft OneDrive. This article describes a simple way to use shadow copies as a work around.

Microsoft provides detailed documentation for the shadow copy service. Basically, a shadow copy is like a snapshot of something on your disk drive that is done in a way to make sure it is in a consistent state. If you just want to do a simple backup or copy a file, the simplest way I found is as follows:

  1. Install ShadowSpawn from GitHub or install chocolatey and use chocolatey to install ShadowSpawn.
    • ShadowSpawn is a simple program that creates a shadow copy, then spawns another program to use the shadow copy and deletes the shadow copy when finished.
  2. Use RoboCopy with ShadowSpawn.

For example, imagine you have a folder at C:\foo\bar which you wish to copy to D:\backups but you are having trouble due to locked files. You can do something like:

ShadowSpawn.exe C:\foo\bar Q: robocopy Q:\ D:\backups

Refinements and Explanation

The command above uses ShadowSpawn to make a shadow copy of C:\foo\bar, put it into the unused drive Q: (pick a different letter if you already have a drive mapped to Q:), use robocopy to do the backup, and then cleanup the shadow copy. See RoboCopy documentation for details on how to control copying as well as other document for forcing robocopy to overwrite existing files.

You can use wmic or vssadmin to do the shadow copy, but it is a bit complicated and simpler to just use ShadowSpawn.

Note that if you used chocolatey, you should check on where chocolatey installs commands (e.g., C:\ProgramData\chocolatey\bin).

Finally, note that ShadowSpawn triggers the User Access Control dialog requiring the user to confirm access. If you want to use the above in a script, there are various ways of avoiding UAC (e.g., via registry settings) but the simplest approach is just to use the Windows Task Scheduler to run your script. Beware that Task Scheduler sometimes fails to show tasks as finished until you manually refresh.