moveFile static method

FileResult<File> moveFile(
  1. String sourcePath,
  2. String targetPath
)

移动文件

Moves a file

Implementation

static FileResult<File> moveFile(String sourcePath, String targetPath) {
  try {
    final sourceFile = File(sourcePath);
    if (!sourceFile.existsSync()) {
      return const FileResult.failure('Source file does not exist');
    }

    final targetFile = File(targetPath);
    // 确保目标目录存在
    final targetDir = targetFile.parent;
    if (!targetDir.existsSync()) {
      targetDir.createSync(recursive: true);
    }

    final movedFile = sourceFile.renameSync(targetPath);
    return FileResult.success(movedFile);
  } catch (e, stack) {
    loge('Failed to move file from $sourcePath to $targetPath: $e\n$stack');
    return FileResult.failure(e.toString());
  }
}