How to split a folder structure into an array with sub arrays?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 7
- Joined: Tue Jan 21, 2020 11:50 pm
How to split a folder structure into an array with sub arrays?
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
how do i convert the chosen folder structure to an array with its sub folders as sub arrays?
thanks
-
- Posts: 7
- Joined: Tue Jan 21, 2020 11:50 pm
Re: How to split a folder structure into an array with sub arrays?
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?
can anyone advise on how to convert this to an array, with it's sub folders converted into sub arrays?
-
- VIP Livecode Opensource Backer
- Posts: 9660
- 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?
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
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
-
- Posts: 7
- Joined: Tue Jan 21, 2020 11:50 pm
Re: How to split a folder structure into an array with sub arrays?
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?
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?
-
- VIP Livecode Opensource Backer
- Posts: 9834
- 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?
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 7
- Joined: Tue Jan 21, 2020 11:50 pm
Re: How to split a folder structure into an array with sub arrays?
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
It won't let me post an external URL but it's the livecode files and folders part 2 tutorial
Thanks
-
- VIP Livecode Opensource Backer
- Posts: 9834
- 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?
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- 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?
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:
but it could be altered to do the following:
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.
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"]
Code: Select all
put _createNodesForFolder(tFolder) into tNodeA["children"]
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
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
Re: How to split a folder structure into an array with sub arrays?
The list of paths has the content for the array, so you can build the array without code to read directories:FourthWorld wrote: ↑Wed Jan 22, 2020 7:15 pm...a list of full paths to the files within the specified folder ...
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
-- Dick
-
- VIP Livecode Opensource Backer
- Posts: 9834
- 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?
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to split a folder structure into an array with sub arrays?
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.
Looking at the code though, they only deal with creating lists - not arrays, so this may or may not help the original poster.
Re: How to split a folder structure into an array with sub arrays?
If I've understood the requirement correctly - I may not have! - this should work.
Is this what you were after?
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