Add a new Module named "Globals" to your project (not to a window)
In that module, add the ItemNames function
Works like a charm.
That's all it takes, really.
@Tim J The MBS method is cross platform while the Xojo declare above is Windows only.
Having said, that, they are on par speed-wise on Windows.
Is speed an issue on OS X using the code in te above method? When I've had to deal with a lot of files it has always been on Windows, not OS X.
@Karen A ChildHandle = FindFirstFileW(f.AbsolutePath + "*.*", ChildData)
One thing I forgot to mention. Note the wildcard file name specifications...
You can substitute (or write a function where you can pass in) any valid file name wildcard string ,and have it only return files with matching names... I typically use that when looking for files with a specific extension.
Of course on other OSes besides windows , you would have to check the filename yourself in Xojo code, but you would anyway unless they have equivalent declares.
@Karen A Is speed an issue on OS X using the code in te above method? When I've had to deal with a lot of files it has always been on Windows, not OS X.
The MBS function is much faster on OS X than the method above. I often have to deal with millions of files in film processing (an average film = 120 minutes of DPX frames at 29.97 frames per second = 12,947,040 files...)
@Gert VAssche Thanks Michel. I saw this but I can't figure out how to map the regular folderitem to the ItemNames function.
The very simple example on that page precisely picks itemName from item to put it into a ListBox. Can you explain more clearly what you are trying to achieve ?
When I have this:
Dim dlg as New SelectFolderDialog Dim f as FolderItem Dim itemIdx as Integer Dim dirCount as Integer Dim item as FolderItem dlg.InitialDirectory=SpecialFolder.Documents f=dlg.ShowModal() If f <> Nil then dirCount = f.Count For itemIdx = 1 to dirCount item = f.Item(itemIdx) If item <> nil Then DataList.addrow item.name End If Next end if
I don't know how to call the ItemNames function.
I also don't see how to use the boolean ReturnFileNames.
If you want it recursively, it's easier to pass it on to a Method so it can call itself when it finds a sub folder.
Dim dlg As New SelectFolderDialog Dim f As FolderItem = dlg.ShowModal() If f <> Nil Then ListFiles(f) End If
Sub ListFiles(theFolder As FolderItem) Dim f As FolderItem If theFolder <> Nil And theFolder.Directory Then Dim items As Integer = theFolder.Count For i As Integer = 1 To items f = theFolder.Item(i) If f.Directory Then ListFiles(f) Else Listbox1.AddRow(f.Name) End Next End If End Sub
See the first line:
Function ItemNames(extends f as folderItem, ReturnFileNames as boolean = true, ReturnFolderNames as boolean = true) As String()
Note where it says "extends f as folderItem".
In your project, add a module. Then inside this module add the Function.
Then you can do something like:
Dim someFolderitem As FolderItem Dim someArray() As String someArray = f.ItemNames(true, true)