简体   繁体   English

将部门结构导出到csv / xl文件

[英]Exporting diectory structure to csv/xl file

my requirement is to enumerate all directories and specific .tif files (that are at the end of the structure). 我的要求是枚举所有目录和特定的.tif文件(位于结构的末尾)。 Sample is 样本为

                         A (path selected from UI) <has>
                   B<has>             and              C<has>
       D <has>         E           F              G             H      I        J
K         L<has>
       1.tif   2.tif

In the above directory, A has B and C. Named as clients. 在上面的目录中,A具有B和C。被命名为客户端。 B has D,E,F (as dated), D has K and L (family). B具有D,E,F(已注明日期),D具有K和L(家族)。 So Ineed your help in retrieving the directory structure in txt or excel file as 因此,Ineed帮助您检索txt或excel文件中的目录结构为

B                  D  
                       K    0
                       L    2 (since there are two tif files)


                   E
                   F

Similary for c and other directories. 与c和其他目录相似。

I am not sure that I understand what you want but here is something that to get you started 我不确定我了解您想要什么,但是这里有一些可以帮助您入门的东西

    private static void ProcessFolder(string folder, string level, string separator, StreamWriter output)
    {
        var dirs = Directory.GetDirectories(folder);
        foreach ( var d in dirs )
        {
            output.Write(level);
            output.WriteLine(d);
            ProcessFolder(d, level + separator, separator, output);
        }
        Console.WriteLine();
        var files = Directory.GetFiles(folder);
        foreach ( var f in files )
        {
            output.Write(level);
            output.WriteLine(f);
        }
    }

You will have to customize it to filter TIFF or whatever you want. 您将必须对其进行自定义以过滤TIFF或任何您想要的内容。 You can call this function like this and it will generate the file 您可以像这样调用此函数,它将生成文件

        using ( var output = new StreamWriter(@"C:\test.csv") )
        {
            ProcessFolder(@"c:\Program files", "", ";", output);
        }

Double-click on the generated file and Excel will probably open :) 双击生成的文件,Excel可能会打开:)

Maybe this would do the trick (or give at least a good start point): 也许这可以解决问题(或至少提供一个很好的起点):

public void OutputStructureToFile(string outputFileName, string folder, string searchPattern)
{
    using (var file = new StreamWriter(outputFileName))
    {
        file.Write(GetStructure(new DirectoryInfo(folder), searchPattern));
    }
}

public string GetStructure(DirectoryInfo directoryInfo, string searchPattern)
{
    return GetStructureRecursive(directoryInfo, searchPattern, 0);
}

private string GetStructureRecursive(DirectoryInfo directoryInfo, string searchPattern, int level)
{
    var sb = new StringBuilder();

    var indentation = level * 5;

    sb.Append(new String(' ', indentation));
    sb.AppendLine(directoryInfo.Name);

    foreach (var directory in directoryInfo.GetDirectories())
    {
        sb.Append(GetStructureRecursive(directory, searchPattern, level+1));
    }

    var groupedByExtension = directoryInfo.GetFiles(searchPattern)
                                          .GroupBy(file => file.Extension)
                                          .Select(group => new { Group = group.Key, Count = group.Count() });

    foreach (var entry in groupedByExtension)
    {
        sb.Append(new String(' ', indentation));
        sb.AppendLine(String.Format("   {0,10} {1,3}", entry.Group, entry.Count));
    }

    return sb.ToString();
}

And if you need it for Excel as a .csv file you should instead use this recursive function 如果您需要Excel作为.csv文件,则应改用此递归函数

private string GetStructureRecursiveForCsv(DirectoryInfo directoryInfo, string searchPattern, int level)
{
    var sb = new StringBuilder();

    var indentation = level;

    sb.Append(new String(';', indentation));
    sb.AppendLine(directoryInfo.Name);

    foreach (var directory in directoryInfo.GetDirectories())
    {
        sb.Append(GetStructureRecursiveForCsv(directory, searchPattern, level+1));
    }

    var groupedByExtension = directoryInfo.GetFiles(searchPattern)
                                          .GroupBy(file => file.Extension)
                                          .Select(group => new { Group = group.Key, Count = group.Count() });

    foreach (var entry in groupedByExtension)
    {
        sb.Append(new String(';', indentation));
        sb.AppendLine(String.Format(";{0};{1}", entry.Group, entry.Count));
    }

    return sb.ToString();
}

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

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