I'm having a little bit of a hard time figuring out an algorithm to index a list of string I have, to be able to order them in a tree hierarchy way.
I have an s3 bucket with thousands of files. The file's names are their path in a windows OS, for example:
root/mainfolder/folder/file.txt
root/mainfolder/folder/file1.txt
root/mainfolder/folder2/file.txt
root/mainfolder/folder3/file1.txt
root/mainfolder2/folder4/file7.txt
root/mainfolder/file.txt
....
I want to create a dropdown tree for all the files.
I will be using this template, so I have been trying to to create an object for each file with the following properties.
templateData.Value = "id";
templateData.Text = "name";
templateData.Expanded = "expanded";
templateData.HasChildren = "hasChild";
templateData.ParentValue = "pid";
My approach so far has been using the split()
funtion to separate each folder and file in single strings.
item = "root/mainfolder/folder/file.txt";
string[] split = item.Split('/');
split [0] = root;
split [1] = mainfolder;
split [2] = folder;
split [3] = file.txt;
Then I iterate the split list, and create an object in a list called DropDownTree
for each folder.
When I get the next item in the s3 folder I would use:
(DropDownTree.FirstOrDefault(x => x.Name == split[i]) == null)
Meaning, if that folder did not existed in the DropDownTree list I am creating, I would created the object, otherwise I would just advance to the next index in the split. But is really not working and is super slow.
Which approaches are best in this case? Has anyonetried an algorithm like this?
Thank you.