skip to Main Content

Given a List of Strings to each file within the structure

var paths = new List<string> {"root_folder/a first folder/holidays.mov", "root_folder/a first folder/javascript-file.js", etc...}

I need to output a String that represents the directory structure

root_folder/
|-- a first folder/
|   |-- holidays.mov
|   |-- javascript-file.js
|   `-- some_picture.jpg
|-- documents/
|   |-- spreadsheet.xls
|   |-- manual.pdf
|   |-- document.docx
|   `-- presentation.ppt
|       `-- test    
|-- empty_folder/
|-- going deeper/
|   |-- going deeper/
|   |   `-- going deeper/
|   |        `-- going deeper/
|   |            `-- .secret_file
|   |-- style.css
|   `-- index.html
|-- music and movies/
|   |-- great-song.mp3
|   |-- S01E02.new.episode.avi
|   |-- S01E02.new.episode.nfo
|   `-- track 1.cda
|-- .gitignore
|-- .htaccess
|-- .npmignore
|-- archive 1.zip
|-- archive 2.tar.gz
|-- logo.svg
`-- README.md

I found some information for the TreeView control but I just need to output a String.
Any help would be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Managed to get the directory structure with this class I've made:

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    /// <summary>
    /// Summary description for CodeComparisonToolDirBuilder
    /// </summary>
    public class CodeComparisonToolDirBuilder
    {
        public string Name { get; private set; }
        public CodeComparisonToolDirBuilder Parent { get; set; }
        public string Hash { get; set; }
        public bool Read { get; set; }
        public bool Write { get; set; }
        public List<CodeComparisonToolDirBuilder> Children { get; private set; }
    
        public CodeComparisonToolDirBuilder(string name, CodeComparisonToolDirBuilder parent)
        {
            Name = name;
            Parent = parent;
            Children = new List<CodeComparisonToolDirBuilder>();
        }
    
        public bool Contains(string name)
        {
            return Children.Any(d => d.Name.Equals(name));
        }
    
        public CodeComparisonToolDirBuilder Get(string name)
        {
            return Children.FirstOrDefault(d => d.Name.Equals(name));
        }
    
        public override string ToString()
        {
            return Name;
        }
    
        public void PrintTree(StringBuilder output, bool isRoot)
        {
            string prefix;
            string pre_0 = "    ";
            string pre_1 = "│   ";
            string pre_2 = "|-- ";
            string pre_3 = "`-- ";
    
            CodeComparisonToolDirBuilder tree = this;
    
            if (tree.Parent != null && !(tree.Equals(tree.Parent.Children.Last())))
            {
                prefix = pre_2;
            }
            else
            {
                prefix = pre_3;
            }
    
            while (tree.Parent != null && tree.Parent.Parent != null)
            {
                if (tree.Parent != null && !(tree.Parent.Equals(tree.Parent.Parent.Children.Last())))
                {
                    prefix = pre_1 + prefix;
                }
                else
                {
                    prefix = pre_0 + prefix;
                }
    
                tree = tree.Parent;
            }
    
            if (isRoot)
            {
                output.AppendLine(this.Name);
            }
            else
            {
                output.AppendLine(prefix + this.Name);
            }
    
            foreach (CodeComparisonToolDirBuilder child in this.Children)
            {
                child.PrintTree(output, false);
            }
        }
        public static void TreeStruct(CodeComparisonToolDirBuilder parent, List<string> edges)
        {
            if (edges.Count == 0) return;
    
    
            List<CodeComparisonToolDirBuilder> matchedChildren = new List<CodeComparisonToolDirBuilder>();
    
            foreach (CodeComparisonToolDirBuilder tree in parent.Children)
            {
                if (tree.Name == edges[0])
                {
                    matchedChildren.Add(tree);
                }
            }
    
            CodeComparisonToolDirBuilder pointer;
    
            if (matchedChildren.Count != 0)
            {
                pointer = matchedChildren[0];
            }
            else
            {
                pointer = new CodeComparisonToolDirBuilder(edges[0], parent);
                parent.Children.Add(pointer);
            }
    
            edges.RemoveAt(0);
            TreeStruct(pointer, edges);
        }
    
    }
    

    To use simply declare a CodeComparisonToolDirBuilder at the root folder/node. This was inspired by Building Tree Structure from a list of string paths


  2. Well, a treeview is kind of ideal, since you can then provide a windows like explorer view, and do so in a web page. the result is a VERY nice interface.

    However, you can also use the windows command line, and use the "tree" command, and it will output files and a tree like structure.

    So, say this:

    enter image description here

    So, you can use shell() command, and capture the output of the windows command "tree", and it does quite a nice job.

    It really depends on what you want to do with the final output.

    Overall, it not a whole lot less code to use a treeview, since as such, they can be recursive.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search