Hello everybody
I’m hitting an outofboundexception. But I have no clue why it happens. See this snippet:
[code]For i As Integer = 0 To Path.Count - 1
If Path.Item(i).Directory = True Then //<---- Error here
//
End If
Next[/code]
So I want to step trough all childs of a folderItem, if it’s a Directory, do something with it.
I have no idea why this happens, as i should always be in range, as calculated by the line above it. I must be overlooking something. Anyone notices my mistake?
Thanks
Try For i As Integer = 1 To Path.Count
NOTE: If you want to iterate over the contents of a directory, make sure to always do this starting at index 1, then increasing the index up to the value of FolderItem.Count. Avoid iterating backwards (using a For…Next loop with DownTo or Step -1), because that can potentially become extremely slow with larger directories, especially on macOS. To see how to delete the contents of a folder, see the example in FolderItem.Delete.
Also make sure when you “do something” to the directories that you are not moving, renaming, deleting them otherwise the counts will be off. One strategy to cope with this is:
For i As Integer = Path.Count DownTo 1
If Path.Item(i).Directory = True Then //<---- Error here
//
End If
Next
Dang, I just overlooked that in the docs. Stupid of me. Thanks man.
Xojo devs: Most (nearly all?) arrays are zero based, what’s the reason this is one based?
That is one of the improvements they hope to make with the new framework. “Consistency” 
Peter is right DownTo does slow down with large directories. However FolderItem gets pretty slow with large directories regardless. MBS plugins have some faster folder methods if you are dealing with LARGE amounts of stuff in a folder. Generally speaking though its not good practice to stuff thousands of items in a single folder. Current filesystems are not well optimized for that use case in any environment.
Good to hear that!
Luckely my use case is more ‘dozens’ and not thousands. Thanks for the snippet with moving/renaming etc. Could be of help in the future. At this moment I’m just reading files. 