removeFilesSync static method
根据指定条件删除文件
TODO 需要实现异步方法
path 需要删除的路径
pattern 过滤器
Implementation
static void removeFilesSync(
String path, {
Pattern? pattern,
}) {
if (!existsSync(path)) {
return;
}
void fnDel(FileSystemEntity entity) {
bool needRemove = true;
final p = entity.path;
if (pattern != null) {
final size = pattern.allMatches(p).length;
needRemove = size > 0;
}
if (needRemove) {
Logger.info('[Remove]', p);
entity.deleteSync();
}
}
final type = typeSync(path);
if (type == FileSystemEntityType.file) {
fnDel(File(path));
} else if (type == FileSystemEntityType.directory) {
// 读取目录,并拿到子项
final files = Directory(path).listSync();
// 循环执行,如果是文件执行删除方法,如果目录则执行递归
for (final f in files) {
if (isFileSync(f.path)) {
fnDel(f);
} else if (isDirectorySync(f.path)) {
removeFilesSync(f.path, pattern: pattern);
}
}
}
}