Skip to content
Yevhen Bobrov edited this page Aug 20, 2014 · 9 revisions
Selecting files and folders
// use object initializer to specify file selection paths
var files = new FileSet
{
    "a.txt",
    "b.txt"
}

// use globbing patterns, like '**' for recursive selection
// and standard wildcards, like '*' and '?' are supported as well
var files = new FileSet
{
    @"Output\**\*.txt",
    "b?.txt"
}

// file set supports TeamCity-style inclusion/exclusion patterns
// the below spec will exclude all files with the name 'b.txt'
var files = new FileSet
{
    @"Output\**\*.txt",
    "-:b.txt"                 
}

// separate multiple patterns with '|' symbol
var files = new FileSet
{
    @"Output\**\*.txt|-:b.txt"
}

// use implicit conversions, to convert 
// from string to file set and vice versa
FileSet textFiles = @"{dir}\*.txt";

// use custom ToString() overload, which takes path separator
var testAssemblies = new FileSet{@"{outputPath}\*.Tests.dll"}.ToString(" ");
Cmd(@"nunit-console.exe {testAssemblies}");

// use implicit conversion from/to string array
string[] files = new FileSet{"*.txt"};
FileSet files = new string[]{"a.txt", "*.rc"};

// you can enumerate file set, which will 
// give you back fully resolved file paths
foreach (var path in new FileSet{"*.txt"})
    Console.WriteLine(path);

// use explicit add methods to manipulate file set
var files = new FileSet();
files.Add("a.txt|b.txt");
files.Add(new[]{"a.txt", "b.txt|c.txt"});

// use explicit inclusion/exclusion methods
var files = new FileSet();
files.Include("*.txt");
files.Exclude("b.txt");

// exclude files by Regex
FileSet files = @"**\*.*";
files.Exclude(new Regex("[0-9]+\.txt"));

// exclude files by custom function
// (refer to FileSet.Item below for details)
FileSet files = @"**\*.*";
files.Exclude(item => item.Extension == "txt");

// you can use Resolve() method to get back
// fully resolved file paths as FileSet.Item structures
// (refer to FileSet.Item below for details)
foreach (var item in new FileSet{@"**\*.txt"}.Resolve())
    Console.WriteLine(item.RecursivePath);

You can also specify base path ala NAnt, when constructing FileSet.

// so instead of this
var files = new FileSet
{
    @"Output\Nake.dll",
    @"Output\Utility.dll"                 
}

// you can do this
var files = new FileSet("Output")
{
    "Nake.dll",
    "Utility.dll"                 
}

You can convert file set to a sequence of MSBuild task items, which then could be directly passed to any MSBuild task, which expects array of task item as input:

using MSBuild.Community.Tasks.Xml;

[Task] void RunNUnit()
{
    MSBuild(new NUnit
    {
         Assemblies = new FileSet {@"*.Tests.dll"}.AsTaskItems(),
         DisableShadowCopy = true
    });
}
Transforming file paths
Mirror directory structure

Having the following hierarchy:

A
|__ F1.txt
|__ F2.txt 
|__ B
   |__ F3.txt
   |__ F4.txt

After running the code below:

FileSet tree = @"**\*;**\*.*";
string[] result = tree.Mirror("Dir");

result will have the following paths:

Dir
|__ A
   |__ F1.txt
   |__ F2.txt 
   |__ B
      |__ F3.txt
      |__ F4.txt
Flatten directory structure

Having the following hierarchy:

A
|__ F1.txt
|__ F2.txt 
|__ B
   |__ F3.txt
   |__ F4.txt

After running the code below:

FileSet tree = @"**\*;**\*.*";
string[] result = tree.Flatten("Dir");

result will have the following paths:

Dir
|__ A
|__ F1.txt
|__ F2.txt 
|__ B
|__ F3.txt
|__ F4.txt
Custom transform function

Having the following hierarchy:

A
|__ F1.txt
|__ F2.txt 
|__ B
   |__ F3.txt
   |__ F4.txt

After running the code below:

FileSet tree = @"**\*;**\*.*";

string[] result = files.Transform((FileSet.Item x) => 
    Path.Combine(@"C:\Dir", x.RecursivePath, x.Name + ".tmp")
);

result will have the following paths:

Dir
|__ A
   |__ F1.tmp
   |__ F2.tmp 
   |__ B
      |__ F3.tmp
      |__ F4.tmp

The custom FileSet.Item structure, that will be passed to transform function, represents fully resolved file/folder path within FileSet and will have the following fields:

Field Description
BasePath Base directory path, not including recursive directory (absolute path)
FullPath Full path in original directory tree (absolute path)
RecursivePath Part of the path captured recursively (relative path)
FileName File name including extension
Name File name without extension
Extension File extension
Clone this wiki locally