Computing a hierarchical tree of files and folders

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
golife
Posts: 103
Joined: Fri Apr 02, 2010 12:10 pm

Computing a hierarchical tree of files and folders

Post by golife » Thu Jul 08, 2021 4:51 pm

Based on the lesson https://lessons.livecode.com/m/4071/l/1 ... ers-part-2 I have a list of files in any depth starting from a root folder.

I would like to present files not with their full path, but as leaves to nodes and nodes as a tree.

The list I receive looks something like this (very long list of folders with files and subfolders with their files):

LPS_App/docs/Table Arrays_LPS.txt
LPS_App/docs/Text and Paragraph formats_LC.txt
LPS_App/docs/USE CASES PARTY.txt
LPS_App/docs/Access Privileges User Rights User Roles/USE CASES PARTY.txt
LPS_App/docs/Access Privileges User Rights User Roles/210412 2200 SQL Script changes for selection.txt

A tree would look like this, for example (or be tab delimited):

..| Root Folder
.....|-- File 1
.....|-- File 2
.....|-- File 3
.....|-- Another Folder
..........|-- File 4
..........|-- File 5
..........|-- File 6

I am kindly asking if there is a ready-made solution around to convert the path information into such hierarchical display that can be used in the datagrid, in tab-delimited fileds, or otherwise formatted.

Thanks in advance, Roland

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Computing a hierarchical tree of files and folders

Post by bwmilby » Fri Jul 09, 2021 5:01 am

If you modify the source to generate a nested array, then the tree view widget could display the data fairly efficiently.

Another alternative is Trevor’s DataView Tree. He has an example that displays a folder hierarchy (GitHub.com/trevordevore/dataview_demo).

Someone may have a nifty way to parse your existing data.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

golife
Posts: 103
Joined: Fri Apr 02, 2010 12:10 pm

Re: Computing a hierarchical tree of files and folders

Post by golife » Fri Jul 09, 2021 4:36 pm

Thank you Brian for the response.

Nested arrays is one way I am looking into. There is even a lesson I was not aware about.
Also I will look into Trevor's solution.

Just for an exercise, I was thinking about a purely list/text based solution without using arrays, but other things came in between. If I find the time, I will post.

https://lessons.livecode.com/m/4071/l/2 ... dable-form

Regards, Roland

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Computing a hierarchical tree of files and folders

Post by FourthWorld » Fri Jul 09, 2021 4:42 pm

That Lesson is still useful for certain things, but since it was written LC now has a Tree widget which displays array contents in outline form in one step.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

golife
Posts: 103
Joined: Fri Apr 02, 2010 12:10 pm

Re: Computing a hierarchical tree of files and folders

Post by golife » Fri Jul 09, 2021 4:50 pm

Hi Brian

Reference to the link you gave: https://github.com/trevordevore/dataview_demo

The DataView Demo from Trevor looks phantastic. The file browser demo displays folders and files exactly what I had in mind.

I have to dig deeper into it to get a full picture.

Roland

stam
Posts: 2635
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Computing a hierarchical tree of files and folders

Post by stam » Sun Jul 11, 2021 3:14 am

golife wrote:
Thu Jul 08, 2021 4:51 pm
I would like to present files not with their full path, but as leaves to nodes and nodes as a tree.
The list I receive looks something like this (very long list of folders with files and subfolders with their files):

LPS_App/docs/Table Arrays_LPS.txt
LPS_App/docs/Text and Paragraph formats_LC.txt
LPS_App/docs/USE CASES PARTY.txt
LPS_App/docs/Access Privileges User Rights User Roles/USE CASES PARTY.txt
LPS_App/docs/Access Privileges User Rights User Roles/210412 2200 SQL Script changes for selection.txt

I am kindly asking if there is a ready-made solution around to convert the path information into such hierarchical display that can be used in the datagrid, in tab-delimited fileds, or otherwise formatted.
Thanks in advance, Roland
Hi Roland,
Trevor's solution is undoubtedly superior, but if you want a quick solution using your existing data, it's a matter of splitting it into a multidimensional array, which was exactly a question Dick helped me with here: viewtopic.php?f=8&t=36071&p=206874#p206874. You can then pass this array to LiveCode's tree view widget, which would appear to be what you requested.

I've abstracted Dick's advice to a function generalisable for any text and work even if there are multiple duplicate keys (in your case directories) containing different values - just pass the text, the delimiter and if you wish the values to be numerically indexed (this is required if there are multiple identical keys with different values as they'll all overwrite each other otherwise).

Code: Select all

function compoundSplit pText, pDelimiter, assignIndexes
    ## if there are duplicate keys (lines) with different values, these need to be numerically keyed -> pass assignIndexes as true
    local tArray, tValue, tSource, x
    lock screen
    filter pText without empty into tSource
    set the itemDelimiter to pDelimiter
    sort lines of tSource text ascending
    repeat with i = 1 to the number of lines of tSource
        if assignIndexes then // assign numeric indexes to values of duplicate keys
            add 1 to x
            put item 1 to -2 of line i of tSource  & pDelimiter & x & pDelimiter & item -1 of line i of tSource into line i of tSource
            if item 1 to -3 of line i of tSource <> item 1 to -2 of line i + 1 of tSource  then
                put 0 into x
            end if
        end if
        put item 1 to -2 of line i of tSource into tArray // series of keys
        put item -1 of  line i of tSource into tValue // value
        split tArray by pDelimiter
        put tValue into it[tArray]
    end repeat
    return it
end compoundSplit
This returns a multidimensional array you can assign to objects, such as a tree view.
If you want to show the example text you post above in a tree view:

Code: Select all

set the arrayData of widget "tree view" to compoundSplit(text, slash, true)
this would present your file paths like this:
tree.jpg

golife
Posts: 103
Joined: Fri Apr 02, 2010 12:10 pm

Re: Computing a hierarchical tree of files and folders

Post by golife » Tue Jul 13, 2021 4:15 pm

@stam

Hello Stam

Sorry, I was out for the last weekend until today and did not reply.

That is really a very helpful comment with a code example and wanted to thank you very much.

In the next days, I will check it out carefully and if there is any thought that might make up a viable contribution, I will write.

Roland

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”