wildcard based file matching

Please share your functions/code-snips

wildcard based file matching

Postby udo.killermann » Sat Jun 02, 2012 11:01 am


I was looking for a way to select files in a directory based on patterns I am knowing from shell commands. So I created a helper that works with predicates to do just that:
Code: Select all
' this is ukHelper.NSObject

Declare Class "NSPredicate"
Declare Function "NSPredicate" - (NSString *)predicateFormat
Declare Function "NSPredicate" - (BOOL)evaluateWithObject:(id)object
Declare Function "NSPredicate" + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat
Declare Function "NSPredicate" + (NSPredicate *)predicateWithValue:(BOOL)value
Declare Function "NSPredicate" - (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)variables
Declare Function "NSMutableArray" - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
Declare Sub "NSMutableArray" - (void)filterUsingPredicate:(NSPredicate *)predicate

Dim thePred As NSPredicate

' here I create the predicate based on a predicate template (this is the better approach)
Function filterArray2(theArray As NSMutableArray, Filter As String) As Array
    Dim localPred As NSPredicate
    Dim localArray As NSMutableArray
    localPred = thePred.predicateWithSubstitutionVariables(Dictionary("Pattern" := Filter))
End Function

' initialize the predicate template
Event Init()
   thePred = NSPredicate.predicateWithFormat("SELF LIKE $Pattern")
End Event

The next part is a utility I use to work with ObjB project directories:
Code: Select all
' this is Global

IBOutlet theTextView As NSTextView

Event AwakeFromNib()

  Dim theProject As String
  ' here I set up a dictionary of the file types I am looking for in specific directories
  Dim items As Dictionary("/objb/":="*.mm", "/objb/Parser/":="*.objb")
  Dim counter As Integer
  Dim itemPath As String
  Dim resultArray As NSMutableArray
  Dim fileCounter As Integer
  Dim fullPath As String
  Dim files As NSMutableArray
  Dim myFileManager As NSFileManager
  theProject = InputPanel("Please enter the name of the project","","")
    ' please enter the path of your Objective Basic projects here
    theProject = "/Users/udo/Desktop/Objective Basic/" & theProject
    For counter = 0 To items.Count() - 1
      itemPath = Concat(theProject, items.Key(counter))

      files = myFileManager.List(itemPath)
      If (files.Count() > 0) Then
        resultArray = ukHelper.filterArray2(files, items.Object(counter))
        For fileCounter = 0 To resultArray.Count() - 1
          fullPath = Concat(itemPath, resultArray.Object(fileCounter))
          theTextView.Append(fullPath & "\n")
      End If
End Event

If you enter a project's name that is located in your Objective Basic project folder it'll show you files matching the supplied patterns for specific directories:
fileMatching.jpg (25.92 KiB) Viewed 1703 times

The project:
(760.09 KiB) Downloaded 165 times

I use ukHelper as a singleton, so I don't have to instantiate it.

MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Return to Code examples

Who is online

Users browsing this forum: No registered users and 1 guest