find function

Future<void> find(
  1. String pattern, {
  2. required LimitedStreamController<FindItem> progress,
  3. bool caseSensitive = false,
  4. bool recursive = true,
  5. bool includeHidden = false,
  6. String workingDirectory = '.',
  7. List<FileSystemEntityType> types = const [Find.file],
})

TODO: restore after 2.16 is released.

Returns the list of files in the current and child directories that match the passed glob pattern.

Each file is returned as an absolute path.

You can obtain a relative path by calling:

var relativePath = relative(filePath, from: searchRoot);

Note: this is a limited implementation of glob. See the below notes for details.

find('*.jpg', recursive:true).forEach((file) => print(file));

List<String> results = find('[a-z]*.jpg', caseSensitive:true).toList();

find('*.jpg'
  , types:[Find.directory, Find.file])
    .forEach((file) => print(file));

Valid patterns are:


[*] - matches any number of any characters including none.

[?] -  matches any single character

[[abc]] - matches any one character given in the bracket

[[a-z]] - matches one character from the range given in the bracket

[[!abc]] - matches one character that is not given in the bracket

[[!a-z]] - matches one character that is not from the range given
 in the bracket

If caseSensitive is true then a case sensitive match is performed. caseSensitive defaults to false.

If recursive is true then a recursive search of all subdirectories (all the way down) is performed. recursive is true by default.

includeHidden controls whether hidden files (.xx) are returned and whether hidden directorys (.xx) are recursed into when the recursive option is true. By default hidden files and directories are ignored. If the wildcard begins with a '.' then includeHidden will be enabled automatically.

types allows you to specify the file types you want the find to return. By default types limits the results to files.

workingDirectory allows you to specify an alternate d irectory to seach within rather than the current work directory.

types the list of types to search file. Defaults to Find.file. See Find.file, Find.directory, Find.link.

Passing a progress will allow you to process the results as the are produced rather than having to wait for the call to find to complete. The passed progress is also returned. If the progress doesn't output stdout then you will get no results back.

TODO(bsutton): consider having find return a Stream and eliminate passing a controller in.

Implementation

// typedef FindController<T> = LimitedStreamController<T>;

///
/// Returns the list of files in the current and child
/// directories that match the passed glob pattern.
///
/// Each file is returned as an absolute path.
///
/// You can obtain a relative path by calling:
/// ```dart
/// var relativePath = relative(filePath, from: searchRoot);
/// ```
///
/// Note: this is a limited implementation of glob.
/// See the below notes for details.
///
/// ```dart
/// find('*.jpg', recursive:true).forEach((file) => print(file));
///
/// List<String> results = find('[a-z]*.jpg', caseSensitive:true).toList();
///
/// find('*.jpg'
///   , types:[Find.directory, Find.file])
///     .forEach((file) => print(file));
/// ```
///
/// Valid patterns are:
/// ```
///
/// [*] - matches any number of any characters including none.
///
/// [?] -  matches any single character
///
/// [[abc]] - matches any one character given in the bracket
///
/// [[a-z]] - matches one character from the range given in the bracket
///
/// [[!abc]] - matches one character that is not given in the bracket
///
/// [[!a-z]] - matches one character that is not from the range given
///  in the bracket
/// ```
///
/// If [caseSensitive] is true then a case sensitive match is performed.
/// [caseSensitive] defaults to false.
///
/// If [recursive] is true then a recursive search of all subdirectories
///    (all the way down) is performed.
/// [recursive] is true by default.
///
/// [includeHidden] controls whether hidden files (.xx) are returned and
/// whether hidden directorys (.xx) are recursed into when the [recursive]
/// option is true. By default hidden files and directories are ignored.
/// If the wildcard begins with a '.' then includeHidden will be enabled
/// automatically.
///
/// [types] allows you to specify the file types you want the find to return.
/// By default [types] limits the results to files.
///
/// [workingDirectory] allows you to specify an alternate d
/// irectory to seach within
/// rather than the current work directory.
///
/// [types] the list of types to search file. Defaults to [Find.file].
///   See [Find.file], [Find.directory], [Find.link].
///
/// Passing a [progress] will allow you to process the results as the are
/// produced rather than having to wait for the call to find to complete.
/// The passed progress is also returned.
/// If the [progress] doesn't output [stdout] then you will get no results
/// back.
///
///TODO(bsutton): consider having find return a Stream and eliminate passing
/// a controller in.
Future<void> find(
  String pattern, {
  required LimitedStreamController<FindItem> progress,
  bool caseSensitive = false,
  bool recursive = true,
  bool includeHidden = false,
  String workingDirectory = '.',
  List<FileSystemEntityType> types = const [Find.file],
}) async =>
    Find()._find(
      pattern,
      caseSensitive: caseSensitive,
      recursive: recursive,
      includeHidden: includeHidden,
      workingDirectory: workingDirectory,
      progress: progress,
      types: types,
    );