1

I have the following code:

var directories =  Directory.GetDirectories(
        environmentSettings.SourcePath, 
        "*",
        SearchOption.AllDirectories)
    .Where(dir => !environmentSettings.FolderExclusions
                                      .Contains(Path.GetFileName(dir)));

I want to create a directory in the target path except for ones that show up in the exclusion list. This works, but only if the directory is directly under the root and does not contain sub-directories.

As an example, if the exclusion list contains a directory called Custom and the root directory is C:\App, it will exclude C:\App\Custom and not create it in the target path, but as soon as it encounters something like C:\App\Custom\Sub, it will end up creating this on the target path.

Can't get it to work for File:

//Copy all the files

var files = Directory.GetFiles(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
      .Select(file => new FileInfo(file))
      .Where(file => !environmentSettings.FolderExclusions
                                         .Contains(file.Name) && 
                     !environmentSettings.FolderExclusions
                                         .Contains(file.Directory.Name));

The problem above, is that I don't know how to tell how nested the file is, for example, if it is under c:\App\Custom\thumbs.db, it works fine, I think, but if it is under c:\App\Custom\sub1\sub2\thumbs.db, it still copies the file and the Custom directory which is what I don't want. I basically need to get the directory right below c:\App and if that is Custom, then I exclude the file.

var files = Directory.GetFiles(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
            .Select(file => new FileInfo(file))
            .Where(file => !environmentSettings.FolderExclusions.Contains(file.Directory.Name) && (file.Directory.Parent == null || !environmentSettings.FolderExclusions.Contains(file.Directory.Parent.Name)));

When I run the following code to loop through all my files and put them in a target directory, I run into a Directory not found exception:

//Copy all the files
foreach (var file in files)
{
File.Copy(file.Name, file.FullName.Replace(environmentSettings.SourcePath, environmentSettings.TargetPath));
}

The problem is that the source file maybe something like \network\app1\one.mp3 and my target directory might be c:\programdata\myapp. It is saying it can't find one.mp3 when copying the files from the source. Not sure how to handle this.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • Regarding the last paragraph: *Is* one.mp3 actually in \network\app1\one.mp3? – Spontifixus Oct 01 '12 at 14:37
  • Yes. That is what makes it confusing, but I am trying to copy it from \network\app1\one.mp3 to c:\programdata\myapp\one.mp3 – Xaisoft Oct 01 '12 at 14:39
  • Ahh - you need to use `File.FullName` in *both* cases... – Spontifixus Oct 01 '12 at 14:43
  • Ah that was it, thanks for your help and patience. I want your opinion. Microsoft.VisualBasic has a method to copy the entire contents of a direct. Discussion can be found here (http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp?lq=1. Do you think it is better to use this? It has the benefit of providing a built-in progress bar, but I am not sure if you can exclude folders/files. I have one more question that I will post in another comment. Again, thanks for your help on this. – Xaisoft Oct 01 '12 at 14:51
  • After I copy all my folders to the target directory. I have a folder on their called `Custom` that has some other folders and files. I want to extract the folders and files from `Custom` and put them in the target directories root folder and then delete the `Custom` folder. Should I use Move or Copy and then Delete in this case? – Xaisoft Oct 01 '12 at 14:52
  • That is up to you. Copy and Delete has the advantage that when Copy fails the source files are still there. When Move fails it could happen (and that happened to me already) that neither the target nor the source file were there... – Spontifixus Oct 01 '12 at 14:55

1 Answers1

3

Try this if this goes just for the parent folder and the first sub folder:

var directories =  Directory.GetDirectories(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
    .Select(dir => new DirectoryInfo(dir))
    .Where(dir=>!excludes.Contains(dir.Name) && (dir.Parent == null || !excludes.Contains(dir.Parent.Name)));

To enable this with files, try the following

var files = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories)
    .Select(file => new FileInfo(file))
    .Where(file=>!excludes.Contains(file.Directory.Name) && (file.Directory.Parent == null || !excludes.Contains(file.Directory.Parent.Name)));
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
  • I assume the DirectoryInfo here is needed for dir.Parent...? – Xaisoft Sep 28 '12 at 20:08
  • Great. This worked. Any idea how I can do the same thing with files. For example, when I do Directory.GetFiles(source,"*",SearchOption.AllDirectories), it failes when it encounters something like Custom\thumbs.db – Xaisoft Sep 28 '12 at 20:22
  • If that worked please mark as answer - and vote up if it helped :) Regarding the files: have a look at the `FileInfo`-class and the properties it provides... – Spontifixus Sep 28 '12 at 20:24
  • I am still not sure how to do it, I updated my post with a tried solution. – Xaisoft Sep 28 '12 at 20:42
  • It fails if the parent is the same as the root because the root has no parent directory and parent is null. – Xaisoft Sep 28 '12 at 21:02
  • So think about how to solve this. Tipp 1: Make sure that parent is not null. Tipp 2: See my edited answer ;) – Spontifixus Sep 29 '12 at 10:38
  • I haven't tried your edited solution yet, but I did try to check that first, but I think I was checking it in the wrong order. I will edit my post to show you what I tried. – Xaisoft Sep 29 '12 at 15:16
  • lol, sorry to put you through this. You were right the first time. I was thinking it was going to create the files, so I assumed it did not work. After I get the folders and files, I needed to loop through them and create them, but now I run into another issue. I will update my post. – Xaisoft Oct 01 '12 at 14:09