Browse folders recursively

Hi,
Does anyone have a routine for iterating through folders recursively?
I am looking to browse the files in each folder (and sub-folders) present in ‘/Users/UserName’ in order to display files whose size exceeds 100MB (for example).
Thanks,

You can do that with folderitem class of course.

Or check alternatives like FileListMBS in MBS Xojo Plugins, which can be much faster.

thanks, i’ll take a look.

Make sure you’re using For Each with the folderitem.children iterator. It’s significantly faster than ChildAt

Isn’t the StackOverflow danger valid any longer?

10 years ago, simple recursion (method calling itself) had to be avoided because the stack would overflow if the folders went too deep.
It has been several years that the answers for this question won’t mention this issue and lead to think it’s now OK to use methods recursion. Something has changed since then?

Your app has several megabytes of stack space.
All the objects are on the heap, so in Xojo you can usually do thousands of recursions without problems.

Thanks.
So this has indeed changed, since ten years ago, a recursion of about 10 sub levels would overflow the stack.

Hi @Christian_Schmitz ,

In the “Util/File List Recursive” example (Method LoadFolderMBS), why when I want to replace the (deprecated) command “if g.Directory(i) then…” with
“if g.IsFolder(i) then …” (or just if g.IsFolder), I get the message:

Type “FileListMBS” has no member named “IsFolder” ???

(but it works under LoadFolderRB)
Thanks

I think you confuse methods in the FolderItem class with methods in FileListMBS class.

1 Like

@Christian_Schmitz

Thank you for your answer. Yes I understand.
Another thing, the FileListMBS class is very fast but it does not seem to support the icons of the elements (files/folders) displayed, unlike the classic “RB” class. Is there a solution to display the icons?

Listbox1.RowImageAt(Me.LastAddedRowIndex) = g.IconMBS(16)
“This method extends class FolderItem, but the base expression is class FileListMBS.”

For such a function, you would ask for the folder item for the entry.

The key thing of FileListMBS is to do things, where you can work on the files without using FolderItem.
e.g. Match files to a criteria and only get FolderItem for a few of them.

If you query FolderItem for each item, you can skip FileListMBS and do FolderItem directly.
The time advantage in FileListMBS is to not make FolderItem objects.

Call this ProcessFolder method and pass it a folder

// ProcessFolder(dir as FolderItem)

for each f as FolderItem in dir.Children
if f.IsFolder and f.Extension = “app” then
// app
elseif f.IsFolder then
// folder
processFolder(f)
elseif f.Visible then
// file
end if
next

If you mark your Code in this Forum as such by using the </> Symbol in the Post-Editor, then the code is much easier to read :slight_smile:

// ProcessFolder(dir as FolderItem)

for each f as FolderItem in dir.Children
if f.IsFolder and f.Extension = “app” then
// app
elseif f.IsFolder then
// folder
processFolder(f)
elseif f.Visible then
// file
end if
next

Thanks. Just testing. The </> didn’t work for me, but I found that three backticks works.

// ProcessFolder(dir as FolderItem)

for each f as FolderItem in dir.Children
if f.IsFolder and f.Extension = “app” then
// app
elseif f.IsFolder then
// folder
processFolder(f)
elseif f.Visible then
// file
end if
next
1 Like

could be a bit hidden in the menu here

To use the button you have to highlight your code first. As you said, I usually use the three backticks instead.

Nope. I always click it and then just paste my Code. :slight_smile:

I don’t see such a Menu.

1 Like

Then it would seem that both options work. I’ve just tried it and paste then highlight and press </> does work. You do run the risk of getting things like " changed to 66 and 99. I always just press the ` button three times.

The menu comes when your window is very narrow.

1 Like

OT
that was my view before and then this button is gone into this sub menu!?
(Windows 11 and Firefox)

If the editor window is narrow then buttons start to hide within the sub menu. Is that your problem?