findFileFromNameAndType function

String? findFileFromNameAndType(
  1. String path,
  2. String name, {
  3. required TypeFile typeFile,
})

Implementation

String? findFileFromNameAndType(
  String path,
  String name, {
  required TypeFile typeFile,
}) {
  path = Structure.replaceAsExpected(path: path);
  if (Directory(path).existsSync()) {
    var splitPath = Structure.safeSplitPath(path);
    splitPath
      ..remove('.')
      ..removeLast();

    var bindingPath = '';
    while (splitPath.isNotEmpty && bindingPath == '') {
      Directory(splitPath.join(separator))
          .listSync(recursive: true, followLinks: false)
          .forEach((element) {
        if (element is File) {
          var fileName = basename(element.path);
          if (fileName == _getFileByType(name: name, typeFile: typeFile)) {
            bindingPath = element.path;
          }
        }
      });
      splitPath.removeLast();
    }
    return bindingPath;
  }
  return null;
}