search(directory:include:exclude:includingPropertiesForKeys:skipHiddenFiles:)
Recursively search the contents of a directory, filtering by the provided patterns
- iOS
- 16.0+
- macOS
- 13.0+
- tvOS
- 16.0+
- watchOS
- 9.0+
func search(directory baseURL: URL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath), include: [Pattern] = [], exclude: [Pattern] = [], includingPropertiesForKeys keys: [URLResourceKey] = [], skipHiddenFiles: Bool = true) -> AsyncThrowingStream<URL, any Error>
Parameters
- baseURL
The directory to search, defaults to the current working directory.
- include
When provided, only includes results that match these patterns.
- exclude
When provided, ignore results that match these patterns. If a directory matches an exclude pattern, none of it’s descendents will be matched.
- keys
An array of keys that identify the properties that you want pre-fetched for each returned url. The values for these keys are cached in the corresponding URL objects. You may specify nil for this parameter. For a list of keys you can specify, see Common File System Resource Keys.
- skipHiddenFiles
When true, hidden files will not be returned.
Returns
An async collection of urls.
Searching is done asynchronously, with each subdirectory searched in parallel. Results are emitted as they are found.
The results are returned as they are matched and do not have a consistent order to them. If you need the results sorted, wait for the entire search to complete and then sort the results.