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.
2
Answers
The first step is to parse your list of paths into a set of objects:
We’ll then start with a dummy root Directory object, which contains all of the files/directories at the root:
We’ll then go through each of the paths and split each into its parts, as you did. We’ll then start walking through the parts, and walking down the directory hierarchy at the same time. So given the path
a/b/c.txt
, we’ll start at theroot
directory that we created above and look for a sub-directory calleda
: if we don’t find one, we’ll create it.We’ll move down a level and look for a sub-directory called
b
on new Directory object fora
we just found/created. Finally, we’ll create a file calledc.txt
on the Directory object forb
.Something like:
Now we’ve got this, we can walk down it and create our template objects as necessary.
See it on SharpLab.
I think it better to parse directly against the file system.
So, say this markup:
So, our code does NOT need to be recursive.
but, we want to have a "thing" that is a folder, and in that folder we have "files", or maybe files and some folders.
So, this code works rather nice:
And we need one more event – the "expand" a folder bit of code.
So this:
And the result is now this: