renameFilesWithSubstring static method
Implementation
static Future<void> renameFilesWithSubstring({
required String path,
required String substring,
required String newName,
}) async {
// NOTE: use an awaited for-loop, not `forEach` with an async callback.
// `forEach` does not await the futures it starts, so the renames run
// fire-and-forget and this method returns before they finish. The next
// steps (content replacement, `dart format`, process exit) then race the
// renames. On fast filesystems the renames usually win; on Windows (slower
// IO, file locking, antivirus) they often lose, leaving `basic_structure`
// files unrenamed and the generated feature broken.
List<FileSystemEntity> entities = Directory(path).listSync(recursive: true);
for (var entity in entities) {
if (entity is File) {
String fileName =
entity.path.split(Platform.pathSeparator).last.toLowerCase();
if (fileName.contains(substring.toLowerCase())) {
String newPath = entity.path
.replaceAll(substring.toLowerCase(), newName.toLowerCase());
await entity.rename(newPath);
}
}
}
}