Mac OS File iteration

Hi, I’m new to Xojo and have been experimenting with it the last couple days. (Coming from C#/VB).

I’ve been reading up on the Xojo Docs. And have been experimenting with File management. (Deleting, Renaming, Moving, Opening, and Iterating through file and file directories). No reason in specific just trying to tinker with it. So Far I’m liking the platform.

I’m trying to iterate through Specific directories, However I can’t seem to use any other directory other than “Documents”. Any other directory throws an error.
I’ve also tried putting the directory in as a string format which also doesn’t work even when trying to use shellpath or Absolutepath;

using Xojo.io Dim f As FolderItem = ("/Users/Library") for each i as FolderItem in f.children Listbox1.AddRow i.name next

So I instead tried the following which also didn’t work:

using Xojo.io Dim f As FolderItem = SpecialFolder.Library for each i as FolderItem in f.children 'if i.IsFolder = false and i.IsVisible then Listbox1.AddRow i.name next

The only directory I can seem to get access to is Documents which seems to work flawlessly:

using Xojo.io Dim f As FolderItem = SpecialFolder.Documents for each i as FolderItem in f.children 'if i.IsFolder = false and i.IsVisible then Listbox1.AddRow i.name next

My last attempt that also did not work;

Dim f As FolderItem f = GetFolderItem("/Library/Application Support", FolderItem.PathTypeShell) for each i as FolderItem in f.child Listbox1.AddRow i.name next End If

Since Xojo isn’t as popular as other languages I can’t find much help other than this forum. Any help is appreciated. I’m just trying to learn as the language seems promising and has great potential. Thanks!

I think you can’t do like that. Try this instead

Dim f As FolderItem = SpecialFolder.Documents
For i as Integer = 1 to f.Count
    Listbox1.AddRow f.Child(i).DisplayName
next

I think there may be confusion between the frameworks. You’re mixing the new and old framework functions in ways that aren’t compatible.

The new framework SpecialFolder only has a handful of locations, while SpecialFolder.Library is a classic framework item.

Your last attempt didn’t work because the path is a NativePath, but you passed PathTypeShell. Also, do note that your last attempt is all classic framework, while the others mix in the new framework.

With new security protocols you likely won’t need more than what’s available with the new framework SpecialFolder for most instances, however if you have a desktop project and are comfortable with the classic framework, you can explore the classic SpecialFolder documentation Be aware though that there are a number of locations in the classic SpecialFolder that have little caveats to them, whereas you should be safe with the newer framework.

As a test, and for practice in getting a FolderItem you have permissions to, use the SelectFolderDialog to ask the user for a directory:

dim dlg as new SelectFolderDialog
dim fSelect as FolderItem = dlg.ShowModal

if fSelect = nil then return

// You'll want to do this count ONLY ONCE so pull it out of the for loop
dim iCount as Integer = f.Count

for i as Integer = 1 to iCount
  ListBoxInstance.AddRow(f.Item(i).Name)

next

Note that this example is entirely classic framework.

Edit: Grammar

try

Dim f As New FolderItem("/Users/Library")

But unless you have a user named “Library” this too will fail.

[quote=372508:@Tim Parnell]I think there may be confusion between the frameworks. You’re mixing the new and old framework functions in ways that aren’t compatible.

The new framework SpecialFolder only has a handful of locations, while SpecialFolder.Library is a classic framework item.

Your last attempt didn’t work because the path is a NativePath, but you passed PathTypeShell. Also, do note that your last attempt is all classic framework, while the others mix in the new framework.

With new security protocols you likely won’t need more than what’s available with the new framework SpecialFolder for most instances, however if you have a desktop project and are comfortable with the classic framework, you can explore the classic SpecialFolder documentation Be aware though that there are a number of locations in the classic SpecialFolder that have little caveats to them, whereas you should be safe with the newer framework.

As a test, and for practice in getting a FolderItem you have permissions to, use the SelectFolderDialog to ask the user for a directory:

dim dlg as new SelectFolderDialog
dim fSelect as FolderItem = dlg.ShowModal

if fSelect = nil then return

// You'll want to do this count ONLY ONCE so pull it out of the for loop
dim iCount as Integer = f.Count

for i as Integer = 1 to iCount
  ListBoxInstance.AddRow(f.Item(i).Name)

next

Note that this example is entirely classic framework.

Edit: Grammar[/quote]

That seemed to have worked, Thanks :slight_smile:

I noticed you said that what you provided is “Classic framework”. Now does that mean it is bad practice to use that method and I should try using a more up to date way of accomplishing what I want or is what you provided considered good practice? The only reason I ask is because When I first start learning a language I don’t want to learn bad habits if I can help it. :stuck_out_tongue:

Thanks for your help!

Nope, at this time it only means that the code won’t work on iOS :slight_smile:

Right now the classic framework is more fleshed out on desktop platforms, this will change over time as Xojo continue to improve the new framework.

Its just a transition state that Xojo is going though at the moment.

Just curious:

when was the last time Xojo improved the new framework ?