csteinhilber
Programmer
I need to build a visual tree from a series of paths, and can't quite get my head around how to do it.
I have a series of paths, like:
/dir1/dir2/dir3/file1.txt
/dir1/dir2/dir4/file2.txt
/dir1/dir5/file3.txt
/dir6/dir7/dir8/file4.txt
and I need the tree to look like:
I want to store the whole thing in an array of arrays... but the sticking point for me is, as I'm recursing through the each path, determining whether entries for a given directory already exists (ie - as I'm building the branch for file2.txt, knowing that /dir1 and /dir2 are already in the array, and that /dir4 should be an additional child of /dir2).
Anybody have an algorithm for this? Seems like it would be a common thing... but maybe not.
Any help would be appreciated.
Thanks in advance,
-Carl
I have a series of paths, like:
/dir1/dir2/dir3/file1.txt
/dir1/dir2/dir4/file2.txt
/dir1/dir5/file3.txt
/dir6/dir7/dir8/file4.txt
and I need the tree to look like:
Code:
dir1
|
---- dir2
| |
| ---- dir3
| | |
| | ---- file1.txt
| |
| ---- dir4
| |
| ---- file2.txt
|
---- dir5
|
---- file3.txt
dir6
|
---- dir7
|
---- dir8
|
---- file4.txt
I want to store the whole thing in an array of arrays... but the sticking point for me is, as I'm recursing through the each path, determining whether entries for a given directory already exists (ie - as I'm building the branch for file2.txt, knowing that /dir1 and /dir2 are already in the array, and that /dir4 should be an additional child of /dir2).
Anybody have an algorithm for this? Seems like it would be a common thing... but maybe not.
Any help would be appreciated.
Thanks in advance,
-Carl