Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#

Zeta Folder XCOPY

Rate me:
Please Sign up or sign in to vote.
4.95/5 (11 votes)
26 Sep 2009CPOL1 min read 52.2K   871   20   17
A small class to perform basic XCOPY like operations from within C#

ZetaFolderXCopy

Introduction

During the process of moving some command scripts to a more robust solution that involved C# Script, I was in the need to replace several XCOPY calls with their equivalents in C#.

What I wanted to avoid is to simply start a CMD.EXE process and call the XCOPY command.

Instead I looked for a class/function in .NET/C# that can serve as a replacement. Although I found some solutions, none of them worked the way I wanted them.

Using the Code

So I decided to develop my own small solution that is no rocket science code but fulfills my requirements.

It has fewer features than XCOPY and is intended for copying folders (the original XCOPY works for both files and folders). In addition it is being designed to be used in a console environment (i.e., the operations are synchronous and blocking).

An example call looks like:

C#
var options =
    new ZetaFolderXCopyOptions
        {
            FilesPattern = "*.*",
            RecurseFolders = true,
            CopyEmptyFolders = true,
            CopyHiddenAndSystemFiles = true,
            OverwriteExistingFiles = true,
            CopyOnlyIfSourceIsNewer = false,
            FoldersPattern = "*"
        }
        .AddExcludeSubStrings(
            "\\.svn\\",
            "\\_svn\\",
            "\\_Temporary\\" );

var xc = new ZetaFolderXCopy();
xc.Copy(
    sourceFolderPath,
    destinationFolderPath,
    options );

The full code consists of a single .CS file and can be downloaded from the top of this article.

Simply drop it into your solution/script and use it. 

Epilog

This article introduced a small class to do basic operations that XCOPY and ROBOCOPY do from within your own C# code.

As always, I would love to get your feedback, suggestions and enhancements. Feel free to drop me a note below in the discussions section of the article.

History

  • 2018-05-27 — Newest snippet is at this Pastebin
  • 2009-09-25 — First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
QuestionVery good snippet. Remark: The author has already a project "Zeta Long Paths" here Pin
Philm_Ed27-Jan-17 7:04
Philm_Ed27-Jan-17 7:04 
GeneralMy vote of 5 Pin
gduncan41121-Dec-12 7:22
gduncan41121-Dec-12 7:22 
Generalthanks, and one probably naive question ... Pin
BillWoodruff26-Sep-09 21:46
professionalBillWoodruff26-Sep-09 21:46 
Hi Uwe,

5 !

Many thanks for this fine example of production code ! fyi (no surprise) : it compiles and works fine in VS 2010 against FrameWork 4.0 in a WinForms project. The usual gotchas can occur if you are copying to some location with a long file path name, and the new file name would exceed the length limit (get ready to write to the root level of a drive if needed ?).

One thing that caught my eye studying your code was the place in the invocation example where you create a new instance of the ZetaFolderXCopyOptions class passing in a {} delimited set of default parameter names and values :

var options = new ZetaFolderXCopyOptions
{
    FilesPattern = "*.*",
    RecurseFolders = true,
    CopyEmptyFolders = true,
    CopyHiddenAndSystemFiles = true,
    OverwriteExistingFiles = true,
    CopyOnlyIfSourceIsNewer = false,
    FoldersPattern = "*"
}
.AddExcludeSubStrings(
    "\\.svn\\",
    "\\_svn\\",
    "\\_Temporary\\" );


I am familiar with optional parameter lists, setting properties in the constructor call, and automatic properties and such, but what did surprise me was the use of the dot after the closing curly brace that then, apparently, invokes a call to AddExcludeSubStrings : that's a usage I haven't seen before, and that's baffling : well back to Troelsen and Richter, and Liberty [or, worst case, MSDN] for me Smile | :)

My "C# mental scanning engine" kept looking in vain, for the required () after the use of 'New !

best, Bill

"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

GeneralRe: thanks, and one probably naive question ... Pin
Uwe Keim26-Sep-09 22:00
sitebuilderUwe Keim26-Sep-09 22:00 
GeneralRe: thanks, and one probably naive question ... Pin
BillWoodruff26-Sep-09 22:33
professionalBillWoodruff26-Sep-09 22:33 
GeneralRe: thanks, and one probably naive question ... Pin
Uwe Keim26-Sep-09 23:00
sitebuilderUwe Keim26-Sep-09 23:00 
GeneralRe: thanks, and one probably naive question ... Pin
BillWoodruff26-Sep-09 23:10
professionalBillWoodruff26-Sep-09 23:10 
GeneralSuggestion regarding Options Pin
Jason Vogel26-Sep-09 10:11
Jason Vogel26-Sep-09 10:11 
GeneralRe: Suggestion regarding Options Pin
Uwe Keim26-Sep-09 10:19
sitebuilderUwe Keim26-Sep-09 10:19 
GeneralRe: Suggestion regarding Options Pin
Jason Vogel26-Sep-09 10:28
Jason Vogel26-Sep-09 10:28 
GeneralRe: Suggestion regarding Options Pin
Uwe Keim26-Sep-09 10:37
sitebuilderUwe Keim26-Sep-09 10:37 
GeneralRe: Suggestion regarding Options [modified] Pin
Jason Vogel26-Sep-09 12:30
Jason Vogel26-Sep-09 12:30 
Questionrobocopy? Pin
christoph brändle26-Sep-09 6:58
christoph brändle26-Sep-09 6:58 
AnswerRe: robocopy? Pin
Uwe Keim26-Sep-09 7:02
sitebuilderUwe Keim26-Sep-09 7:02 
Questionmultiple file/folder filters? Pin
Huisheng Chen26-Sep-09 1:49
Huisheng Chen26-Sep-09 1:49 
AnswerRe: multiple file/folder filters? Pin
Uwe Keim26-Sep-09 2:31
sitebuilderUwe Keim26-Sep-09 2:31 
GeneralRe: multiple file/folder filters? Pin
Huisheng Chen26-Sep-09 4:06
Huisheng Chen26-Sep-09 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.