patrickkidd patrickkidd - 3 years ago 208
iOS Question

How to find all files in subdirectories with NSMetadataQuery?

I am trying to watch all sub-directories starting with my document package bundle on macOS, but I can only get NSMetadataQuery to report files in the immediate directory used for the scope. It does not do a recursive search.

I am trying to watch for a list of files and their download status contained within my file package (NSFileWrapper *) which is stored on iCloud Drive (or anywhere the local FS), but I can only get NSMetadataQuery to return the files directly in the directory passed as a scope. It doesn't do a recursive search.

Here is the only thing I could get working:

_query = [[NSMetadataQuery alloc] init];
NSString *root = [_DocRoot path];
[_query setSearchScopes:[NSArray arrayWithObject:root]]; // /Users/patrick/MyFDPackageBundles
[_query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*.fd'", NSMetadataItemFSNameKey]];

The following properly lists files in subdirectories on macOS and iOS when the search scope is an iCloud container, but returns nothing when run on an arbitrary folder on MacOS:

[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]

Does anyone know how to query for all files in a folder on the filesystem? The apple docs say to use "NSMetadataItemFSNameKey == *" but that throws an exception.

Thank you!

Answer Source
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download