简体   繁体   English

排除包含子目录的目录?

[英]Exclude directories that contain sub directories?

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. 例如,如果排除列表包含一个名为Custom的目录,并且根目录是C:\\App ,它将排除C:\\App\\Custom而不是在目标路径中创建它,但是一旦遇到像C:\\App\\Custom\\Sub这样的东西C:\\App\\Custom\\Sub ,它最终会在目标路径上创建它。

Can't get it to work for File: 无法让它适用于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. 上面的问题是,我不知道如何判断文件是如何嵌套的,例如,如果它位于c:\\App\\Custom\\thumbs.db ,它可以正常工作,我想,但如果是c:\\App\\Custom\\sub1\\sub2\\thumbs.db ,它仍然复制文件和自定义目录,这是我不想要的。 I basically need to get the directory right below c:\\App and if that is Custom , then I exclude the file. 我基本上需要在c:\\App下面找到目录,如果是Custom ,那么我排除文件。

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: 当我运行以下代码循环遍历所有文件并将它们放在目标目录中时,我遇到了Directory not found异常:

//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. 问题是源文件可能是\\ network \\ app1 \\ one.mp3,我的目标目录可能是c:\\ programdata \\ myapp。 It is saying it can't find one.mp3 when copying the files from the source. one.mp3在从源复制文件时找不到one.mp3 Not sure how to handle this. 不知道如何处理这个问题。

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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM