views:

48

answers:

2

Hi all, my requirement is to enumerate all directories and specific .tif files (that are at the end of the structure). 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. B has D,E,F (as dated), D has K and L (family). So Ineed your help in retrieving the directory structure in txt or excel file as

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


                   E
                   F

Similary for c and other directories.

+1  A: 

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. 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 :)

Robert Vuković
+1  A: 

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

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();
}
Oliver