renameRecursive method

void renameRecursive({
  1. required String origin_name,
  2. required String new_name,
  3. bool caseSensitiveFile = true,
  4. List<String> ignoreDirList = const [],
  5. List<String> ignoreFileList = const [],
})

Implementation

void renameRecursive({
  required String origin_name,
  required String new_name,
  bool caseSensitiveFile = true,
  final List<String> ignoreDirList = const [],
  final List<String> ignoreFileList = const [],
}) {
  List<FileSystemEntity> fileSystemEntitys = listSync();
  for (var i = 0; i < fileSystemEntitys.length; i++) {
    FileSystemEntity entity = fileSystemEntitys[i];
    try {
      if (entity is Directory) {
        if (ignoreDirList.contains(path.basename(entity.path))) {
          continue;
        }
        if (RegExp(origin_name, caseSensitive: false)
            .hashData(path.basename(entity.path))) {
          var file_path = entity.path.replaceAll(
              RegExp(origin_name, caseSensitive: false), new_name);
          entity.renameSync(file_path);
          Directory(file_path).renameRecursive(
              origin_name: origin_name,
              new_name: new_name,
              ignoreDirList: ignoreDirList,
              ignoreFileList: ignoreFileList);
        } else {
          entity.absolute.renameRecursive(
              origin_name: origin_name,
              new_name: new_name,
              ignoreDirList: ignoreDirList,
              ignoreFileList: ignoreFileList);
        }
      } else if (entity is File) {
        if (ignoreFileList.contains(path.basename(entity.path))) {
          continue;
        }
        try {
          String data = entity.readAsStringSync();
          if (RegExp(origin_name, caseSensitive: caseSensitiveFile)
              .hashData(data)) {
            entity.writeAsStringSync(data.replaceAll(
                RegExp(origin_name, caseSensitive: caseSensitiveFile),
                new_name));
          }
        } catch (e) {}

        if (RegExp(origin_name, caseSensitive: false)
            .hashData(path.basename(entity.path))) {
          var file_path = entity.path.replaceAll(
              RegExp(origin_name, caseSensitive: false), new_name);
          entity.renameSync(file_path);
        }
      }
    } catch (e, stack) {}
  }
  return;
}