Most of us (I mean developers using SVN as version control system) at some point or the other have needed to copy a versioned folder/folders recursively to another location without those “.svn” directories being copied, right. For me, this is a pretty common task every few weeks when I backup my project files or when I need to sync source files between multiple repos in a highly distributed environment.

TortoiseSVN provides a handy “Export” option for it, but I wanted to automate the task and in some instances gain more control over what was being copied/exported. I have been using an “xcopy” command-based approach for sometime now for this purpose and it was today when I was discussing the approach with a colleague that it appeared to me that I can well create a blog post for it.

“xcopy” is a very versatile command available in Windows command prompt. I would advice you open a Windows command prompt and try executing this there:

 

help xcopy

You should have a long listing of various options available with the xcopy command. One very interesting option is the “/exclude” option. Here is what help says about this option:

 

Specifies a list of files containing strings.  Each string
should be in a separate line in the files.  When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied.  For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.

Does the above explanation ring a bell in your mind? Did you notice that you can put exclude patterns in a text file, and specify that file to xcopy while copying directories, xcopy will then exclude all files/folders during the copy operation that match any of the specified patterns.

Here is an example of how one of my such text files look like (the name of the text file I chose was “sync-exclude.txt”):

 

\.svn\
\obj\

This means anything below “obj” or “.svn” folders would be excluded during copy.

And here’s the xcopy command itself:

 

xcopy "C:\Source Path" "F:\Destination Path" /E /Y /R /Q /exclude:sync-exclude.txt

Here’s an explanation of the various switches I have used:

  1. /E – Copies directories and subdirectories, including empty ones.
  2. /Y – Suppresses prompting to confirm you want to overwrite an existing destination file.
  3. /R – Overwrites read-only files.
  4. /Q – Does not display file names while copying.
  5. /exclude: – The name of text file containing patterns which when match a file/folder name, the file/folder would not be copied.

There are whole lots of other options that you can use (e.g. copying only archive attribute set files, prompting for overwriting etc). In fact I use such “exclude files” together with elaborate batch (.bat) files to automate much of my syncing needs for files which involve versioned directories.