Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building tree from list/path

Status
Not open for further replies.

csteinhilber

Programmer
Aug 2, 2002
1,291
US
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:
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
 
I think you need a class TreeNode with members Name (String) and Contents (Array of TreeNode).

Your main array will contain objects for dir1 and dir6. The dir1 "object" will have "dir1" in the Name member and objects for dir2 and dir5 in the Contents.

Otherwise, you've got to have 2 array items for each item:

Code:
[
  "dir1" 
    [
    "dir2" ["dir3" ["file1" []] "dir4" ["file2" []]] 
    "dir5" ["file3" [] ]
    ] 
  "dir6" ["dir7" ["dir8" ["file4" []]]]
]

Note that the files have to have an empty array after them to show that they do not have subelements.



 
Miros,

Thank you for your response. Yes, I am, in essence, building my own TreeNode class.

But what I'm struggling with is how to build up the data structure. I can hard-code it just fine, and come up with a result that works to build the tree visually.

But I'm having trouble figuring out how to recurse over a series of paths to build the data dynamically. It seems like it's something that should be done all the time (a lot of trees display the structures of file systems, etc)... but, currently, I'm stymied.


-Carl
 
Are you going to try reading the file system to get the lists of folders and files?
Are you trying to list folders/files on the server or on the client PC? I ask because there are issues with both that you should be aware of before spending a significant amount of time on the project.

For breaking down the tree structure though here is the general approach.

Execute a function that returns a list of all objects at a specified path. Setup a loop to go through that list one at a time and output the first folder name then call the same function passing it the name of the first folder object inside it you want to check for subfolders.

Each time the function finds a sub folder of the current object it calls the function again to get a list of those sub objects then calls the function again for a list of objects below that level, etc, etc.

Here is an example in VBA just so you can see how it works. I do not have code in Javascript.
Code:
Sub GetSubfolders(folder)
  Dim oSubfolder
  'Creates a reference to the primary folder you are working with.
  Set objFolder = fso.GetFolder(folder)
  'Grabs the list of subfolders in the current primary folder.
  Set objSubfolders = objFolder.Subfolders
  'Grabs a list of files within the current primary folder.
  Set objFiles = objFolder.Files

  'Setup a loop to look through every subfolder from the current primary.
  For Each oSubfolder in objSubfolders
    'Recursively go down the directory tree
    GetSubfolders(oSubfolder.Path)
  Next
End Sub

This code is not outputting anything, it just recurses through the folders. I actually call another function that deletes files in those folders based on their last modified date. I dropped that function call out to keep things simpler.



At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top