find function

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

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:

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.

Implementation

FindProgress find(
  String pattern, {
  bool caseSensitive = false,
  bool recursive = true,
  bool includeHidden = false,
  String workingDirectory = '.',
  Progress? progress,
  List<FileSystemEntityType> types = const [Find.file],
}) {
  progress ??= Progress.devNull();

  // final controller = StreamController<FindItem>();
  // try {
  //   controller.stream.listen((item) => progress!.addToStdout(item.pathTo));
  //   waitForEx(
  //     core.find(
  //       pattern,
  //       caseSensitive: caseSensitive,
  //       recursive: recursive,
  //       includeHidden: includeHidden,
  //       workingDirectory: workingDirectory,
  //       progress: controller.sink,
  //     ),
  //   );
  // } finally {
  //   controller.close();
  // }

  return FindProgress(
    pattern,
    caseSensitive: caseSensitive,
    recursion: recursive,
    includeHidden: includeHidden,
    workingDirectory: workingDirectory,
    types: types,
  );
}