How to split a folder structure into an array with sub arrays?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
theowright2020
Posts: 7
Joined: Tue Jan 21, 2020 11:50 pm

How to split a folder structure into an array with sub arrays?

Post by theowright2020 » Wed Jan 22, 2020 1:36 pm

i am trying to populate a tree view with a folder structure chosen from disc

how do i convert the chosen folder structure to an array with its sub folders as sub arrays?

thanks

theowright2020
Posts: 7
Joined: Tue Jan 21, 2020 11:50 pm

Re: How to split a folder structure into an array with sub arrays?

Post by theowright2020 » Wed Jan 22, 2020 3:20 pm

I have attached a screenshot of how the folder data is pulled in, before it is split into an array

can anyone advise on how to convert this to an array, with it's sub folders converted into sub arrays?
Attachments
data1.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to split a folder structure into an array with sub arrays?

Post by dunbarx » Wed Jan 22, 2020 5:44 pm

Hi.

Not sure what you mean. if you take your list and split it by return, you will get an array where each key references a line in that list. As is, the keys will be numbered. So you would have, in the array variable "yourArrayHere":

1 Music
2 git_new
3 BoostNote
...

Is this what you wanted?

Craig

theowright2020
Posts: 7
Joined: Tue Jan 21, 2020 11:50 pm

Re: How to split a folder structure into an array with sub arrays?

Post by theowright2020 » Wed Jan 22, 2020 6:04 pm

the list of items

Music
git_new
Boostnote
etc.

are actually folders, which contain other files and folders within them

i am passing them as an array via pFolder into the following code:

' ' '
function listFiles pFolder, pRecurse
local tTotalFiles, tCurrentFiles, tFolders

set the defaultFolder to pFolder
put filteredFiles() into tCurrentFiles

if not pRecurse then
return tCurrentFiles
end if

if tCurrentFiles is not empty then
put tCurrentFiles & return after tTotalFiles
end if

put filteredFolders() into tFolders
repeat for each line tFolder in tFolders
put listFiles((pFolder & slash & tFolder), pRecurse) into tCurrentFiles
if tCurrentFiles is not empty then
put tCurrentFiles & return after tTotalFiles
end if
end repeat
delete the last char of tTotalFiles

return tTotalFiles
end listFiles

' ' '

it throws back an error to say the recursion limit has been exceeded, do you possibly know why?? i appreciate it's an essentially an infinite loop, but any idea whats wrong?

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

Re: How to split a folder structure into an array with sub arrays?

Post by FourthWorld » Wed Jan 22, 2020 6:57 pm

1. Please use the Code tag around code to make it more readable.

2. What is the URL to that tutorial? I'll suggest a fix.

3. The fix is related to permissions: some folders can't be traversed because of restricted permissions or other considerations. To account for that we need to add a line of error-checking after the call to obtain "the files". Without that the routine will continue to attempt the same access over and over, resulting in the recursion error. I'll be able to point out where the error lies once I see the full code listing at the URL you'll provide.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

theowright2020
Posts: 7
Joined: Tue Jan 21, 2020 11:50 pm

Re: How to split a folder structure into an array with sub arrays?

Post by theowright2020 » Wed Jan 22, 2020 7:03 pm

Apologies for the poor code post

It won't let me post an external URL but it's the livecode files and folders part 2 tutorial


Thanks

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

Re: How to split a folder structure into an array with sub arrays?

Post by FourthWorld » Wed Jan 22, 2020 7:15 pm

Thanks, Theo. I added a comment there suggesting revision, but the code is not only missing adequate error checking, but uses a needlessly cumbersome style.

I have a function I use that I can dig up, but none of this will help you build an array to populate a tree widget.

These types of functions generally return a list of full paths to the files within the specified folder. Because the number of nested folders is both unknown in advance and varied, no simple split command can turn that into a nested array.

Indeed, the variable depth poses a challenge for any solution I can think of that isn't dependent on using the "do" command. "Do" can be helpful when no other more with-the-grain option is available, but is notoriously slow and difficult to debug/maintain compared to inline expressions.

Ideally you wouldn't even need to build the full array in advance, but only as needed when each folder in the tree is opened.

I'll think about this to see if I can come up with a graceful solution that's clear and efficient. In the meantime, perhaps one of our resident geniuses will come up with one even sooner.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: How to split a folder structure into an array with sub arrays?

Post by trevordevore » Wed Jan 22, 2020 9:08 pm

theowright2020: I am going to provide some links to some files that may prove helpful. I have a folder browser demo in a DataView demo stack (DataView is a control I created and it has a tree variant). I haven't looked at the type of array that a tree widget requires but you should be able to modify the code to suit.

Here is how I tackle the problem in the DataView Tree:

1. Select a folder
2. Create an array of nodes for the files and folders in the folder.
3. Assign the array of nodes to the DataView Tree and display.
4. If user clicks on a folder node to expand it in the tree then create an array of nodes for the folder and assign the array to the "children" key of parent folder node (that is how the DataView Tree control displays child nodes).

If you wanted the code that creates nodes for a folder to create the entire tree, including all descendants of root folders, then it could be made recursive by having the `_createNodesForFolder` function call itself for each folder that it adds. Currently it does the following:

Code: Select all

put "empty" into tNodeA["children"]
but it could be altered to do the following:

Code: Select all

put _createNodesForFolder(tFolder) into tNodeA["children"]
DataView Demo Github Repo

https://github.com/trevordevore/dataview_demo

Scroll down to the Usage section for a download link and instructions on opening the project in LC 9. Here is a link to the code that that builds the nodes for a folder:

https://github.com/trevordevore/levureh ... codescript

Look for the `_createNodesForFolder` function in that file.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: How to split a folder structure into an array with sub arrays?

Post by rkriesel » Thu Jan 23, 2020 7:50 am

FourthWorld wrote:
Wed Jan 22, 2020 7:15 pm
...a list of full paths to the files within the specified folder ...
The list of paths has the content for the array, so you can build the array without code to read directories:

Code: Select all

function arrayFromPaths pPaths
   local tArray, tFile
   set the itemDelimiter to "/"
   repeat for each line tPath in pPaths
      put last item of tPath into tFile
      delete last item of tPath
      split tPath by slash
      put tFile into tArray[ tPath ]
   end repeat
   return tArray
end arrayFromPaths
How's that?
-- Dick

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

Re: How to split a folder structure into an array with sub arrays?

Post by FourthWorld » Fri Jan 24, 2020 3:07 am

Thanks for chiming in, Dick. Always good to see you here.

I tried your handler, but only got one array element. It was properly nested, but my folder list had a good many folders and files in it. If I modify the "into" to "&cr after" I at least get all the files in the one folder that shows, but none of the other folders.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

kdjanz
Posts: 300
Joined: Fri Dec 09, 2011 12:12 pm
Location: Fort Saskatchewan, AB Canada

Re: How to split a folder structure into an array with sub arrays?

Post by kdjanz » Fri Jan 24, 2020 6:01 am

While looking through the LiveCode Lessons manual on another topic I came across the word from the mothership on how this file stuff should be done with recursion. In the LiveCode Resources Center, under Tutorials/GeneralFiles and Folders, there is a section with the rest of the code that is part of the fragments that have been posted here - including the missing "filteredFilesWithPaths" functions etc. Cut and Paste to your hearts content.

Looking at the code though, they only deal with creating lists - not arrays, so this may or may not help the original poster.

Image
Attachments
Files & Folders.jpg

benr_mc
Posts: 26
Joined: Mon Apr 17, 2006 9:22 pm

Re: How to split a folder structure into an array with sub arrays?

Post by benr_mc » Tue Mar 03, 2020 5:52 pm

If I've understood the requirement correctly - I may not have! - this should work.

Code: Select all

-- return a nested array representing the files and folders of a tree of folders
-- invoke with single parameter, full path to the root folder
--
function directoryTreeAsArray tRoot, tSubPath --> aTree
   local aTree
   set the defaultFolder to tRoot & "/" & tSubPath
   put the files into aTree["files"]
   repeat for each line f in the folders
      if char 1 of f = "." then next repeat -- unix gotcha
      put directoryTreeAsArray(tRoot, tSubPath & "/" & f) into aTree["folders"][f]
   end repeat
   return aTree
end directoryTreeAsArray
Is this what you were after?

Post Reply

Return to “Talking LiveCode”