Wildcard File Searches Declares?

On the Windows side In the Windows Functionality Suite Aaron had declares that let you search a directory using a wildcarded file spec . ( Things like “Prefix*Suffix.ext”) and on Windows it’s very fast.

Are there such declares for OS X? If so can someone share them?

I would like to write an Xplatform (Win & OS X) method to do that. I often run into the need to pull out specific files from a directory with a a lot of files like that. On Windows it’s awfully slow without declares, and ideally i would want to be able to use the same file spec X-Platform.

Thanks,

  • Karen

Mac doesn’t have such a wildcard feature as far as I know.

You should probably use declares for getting the directory contents on Mac as well, then use RegEx to search for your files.

or use FileListMBS class if you use our MBS Plugins. (now updated for Linux and much quicker in upcoming release)

Thanks…

Thinking more about it, I can probably shell out and use the Unix DIR command with the same wildcards to get the file names and then make folder items from the names.

Not sure of the details of doing that (I have not really used the shell object) , or if it would be faster than just iterating through directory in Xojo, but it should be doable.

  • Karen

You use NSPredicate (very powerful!). Like this:

[code]Declare Function NSClassFromString Lib “Cocoa” (aClassName As CFStringRef) As Ptr
Declare Function defaultManager Lib “Cocoa” Selector “defaultManager” (NSFileManagerClass As Ptr) As Ptr
Declare Function contentsOfDirectoryAtPath Lib “Cocoa” Selector “contentsOfDirectoryAtPath:error:” _
(NSFileManager As Ptr, path As CFStringRef, NSError As Ptr) As Ptr
Declare Function predicateWithFormat Lib “Cocoa” Selector “predicateWithFormat:” _
(NSPredicateClass As Ptr, format As CFStringRef) As Ptr
Declare Function filteredArrayUsingPredicate Lib “Cocoa” Selector “filteredArrayUsingPredicate:” _
(NSArray As Ptr, NSPredicate As Ptr) As Ptr
Declare Function count Lib “Cocoa” Selector “count” (NSArray As Ptr) As Integer
Declare Function objectAtIndex Lib “Cocoa” Selector “objectAtIndex:” _
(NSArray As Ptr, index As Integer) As CFStringRef

Static NSFileManagerClassPtr As Ptr = NSClassFromString(“NSFileManager”)
Static NSPredicateClassPtr As Ptr = NSClassFromString(“NSPredicate”)

Dim fileManagerPtr As Ptr = defaultManager(NSFileManagerClassPtr)
Dim directoryContentsArrayPtr As Ptr = contentsOfDirectoryAtPath(fileManagerPtr, “/Users/…/Documents”, Nil)
Dim predicatePtr As Ptr = predicateWithFormat(NSPredicateClassPtr, “self ENDSWITH ‘.pdf’”)
Dim pdfArrayPtr As Ptr = filteredArrayUsingPredicate(directoryContentsArrayPtr, predicatePtr)

For index As Integer = 0 To count(pdfArrayPtr) - 1
System.DebugLog objectAtIndex(pdfArrayPtr, index)
Next[/code]
Replace the dots in the path “/Users/…/Documents” with your username ("~/Documents" will not work).

The man pages for ls says not to attempt to parse it’s output.
If you have MBS I would use Christian’s recommendation, or use declares.

I don’t expect the predicate to be faster than doing it in Xojo with string compare.
(and you may want to handle uppercase extension, too)

I believed using declares (or MBS) to get the file list is faster than Xojo FolderItem, especially with large folders.
Is it not? I can’t remember where I picked up that notion.

Sorry, I do not see anything of that kind at https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ls.1.html

Shell is usually extremely well optimized, so very fast. At any rate, much, much faster than a Xojo FolderItem iteration.