Filtering using NSPredicate
Posted: Fri Jul 14, 2017 6:35 am
Show all mp3 files with duration in the current directory and its subdirectories.
None recursive alternative
If you want both mp3 and wav files, you can change the predicate format string from
@"pathExtension ==[c] 'mp3'"
to
@"lowercase(pathExtension) IN {'mp3','wav'}"
Code: Select all
; >>> Recursively list all mp3 files in the current directory <<<
; >> Some constants from the NSFileManager class <<
EnumerationBinary
#NSDirectoryEnumerationSkipsSubdirectoryDescendants
#NSDirectoryEnumerationSkipsPackageDescendants
#NSDirectoryEnumerationSkipsHiddenFiles
EndEnumeration
; >> Directory to examine recursively <<
Directory.s = GetCurrentDirectory()
; >> Enumerate all directory objects (subdirectories included) <<
Enumerator.i = CocoaMessage(0, CocoaMessage(0, 0, "NSFileManager defaultManager"), "enumeratorAtURL:",
CocoaMessage(0, 0, "NSURL fileURLWithPath:$", @Directory),
"includingPropertiesForKeys:", #nil,
"options:", #NSDirectoryEnumerationSkipsPackageDescendants |
#NSDirectoryEnumerationSkipsHiddenFiles,
"errorHandler:", #nil)
; >> Filter it for mp3 files <<
FilteredArray.i = CocoaMessage(0, CocoaMessage(0, Enumerator, "allObjects"),
"filteredArrayUsingPredicate:",
CocoaMessage(0, 0, "NSPredicate predicateWithFormat:$",
@"pathExtension ==[c] 'mp3'",
"argumentArray:", #nil))
; >> Get the number of mp3 files <<
FilteredArrayCount.i = CocoaMessage(0, FilteredArray, "count")
; >> Show path and duration for all mp3 files <<
i.i = 0
While i < FilteredArrayCount
NSURLObject = CocoaMessage(0, FilteredArray, "objectAtIndex:", i)
NSSound = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"),
"initWithContentsOfURL:", NSURLObject, "byReference:", #YES)
CocoaMessage(@Mp3Duration.d, NSSound, "duration")
CocoaMessage(0, NSSound, "release")
Mp3Path.s = PeekS(CocoaMessage(0, CocoaMessage(0, NSURLObject, "path"), "UTF8String"), -1, #PB_UTF8)
Debug Mp3Path + " [" + FormatDate("%hh:%ii:%ss", Mp3Duration) + "]"
i + 1
Wend
Code: Select all
; >>> List all mp3 files in the current directory <<<
; >> Some constants from the NSFileManager class <<
EnumerationBinary
#NSDirectoryEnumerationSkipsSubdirectoryDescendants
#NSDirectoryEnumerationSkipsPackageDescendants
#NSDirectoryEnumerationSkipsHiddenFiles
EndEnumeration
; >> Directory to examine none recursively <<
Directory.s = GetCurrentDirectory()
; >> Unfiltered array of directory objects <<
UnfilteredArray.i = CocoaMessage(0, CocoaMessage(0, 0, "NSFileManager defaultManager"), "contentsOfDirectoryAtURL:",
CocoaMessage(0, 0, "NSURL fileURLWithPath:$", @Directory),
"includingPropertiesForKeys:", #nil,
"options:", #NSDirectoryEnumerationSkipsPackageDescendants |
#NSDirectoryEnumerationSkipsHiddenFiles,
"error:", #nil)
; >> Filter it for mp3 files <<
FilteredArray.i = CocoaMessage(0, UnfilteredArray,
"filteredArrayUsingPredicate:",
CocoaMessage(0, 0, "NSPredicate predicateWithFormat:$",
@"pathExtension ==[c] 'mp3'",
"argumentArray:", #nil))
; >> Get the number of mp3 files <<
FilteredArrayCount.i = CocoaMessage(0, FilteredArray, "count")
; >> Show path and duration for all mp3 files <<
i.i = 0
While i < FilteredArrayCount
NSURLObject = CocoaMessage(0, FilteredArray, "objectAtIndex:", i)
NSSound = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"),
"initWithContentsOfURL:", NSURLObject, "byReference:", #YES)
CocoaMessage(@Mp3Duration.d, NSSound, "duration")
CocoaMessage(0, NSSound, "release")
Mp3Path.s = PeekS(CocoaMessage(0, CocoaMessage(0, NSURLObject, "path"), "UTF8String"), -1, #PB_UTF8)
Debug Mp3Path + " [" + FormatDate("%hh:%ii:%ss", Mp3Duration) + "]"
i + 1
Wend
@"pathExtension ==[c] 'mp3'"
to
@"lowercase(pathExtension) IN {'mp3','wav'}"