Wednesday, October 22, 2008

Recursive Copy ala LINQ

{

Tonight I needed to copy a bunch of images, no matter their level in the hierarchy to a single folder. Cake walk with C# and LINQ, I'll post the Python equivalent tomorrow.

string sourceDir = @"C:\thesource";
string targDir = @"c:\thetarget";
var files = new DirectoryInfo(sourceDir).GetFiles("*", SearchOption.AllDirectories)
.Where(p=> !p.Extension.Contains("db")) // exclude the pesky windows db file
.Select(p => p.FullName);
foreach (string path in files) {
File.Copy(path, Path.Combine(targDir, Path.GetFileName(path)));
}


 



}

No comments: