From 36848899f001d51b53e014ba1904ddc8e97e8907 Mon Sep 17 00:00:00 2001 From: ionite34 Date: Thu, 4 Jan 2024 18:07:59 +0800 Subject: [PATCH] Add Move and Copy methods for DirectoryPath --- .../Models/FileInterfaces/DirectoryPath.cs | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs b/StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs index 088cf592..d07f18dc 100644 --- a/StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs +++ b/StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs @@ -125,6 +125,98 @@ public class DirectoryPath : FileSystemPath, IPathObject, IEnumerable public Task DeleteAsync(bool recursive) => Task.Run(() => Delete(recursive)); + private void ThrowIfNotExists() + { + if (!Exists) + { + throw new DirectoryNotFoundException($"Directory not found: {FullPath}"); + } + } + + public void CopyTo(DirectoryPath destinationDir, bool recursive = true) + { + ThrowIfNotExists(); + + // Cache directories before we start copying + var dirs = EnumerateDirectories().ToList(); + + destinationDir.Create(); + + // Get the files in the source directory and copy to the destination directory + foreach (var file in EnumerateFiles()) + { + var targetFilePath = destinationDir.JoinFile(file.Name); + file.CopyTo(targetFilePath); + } + + // If recursive and copying subdirectories, recursively call this method + if (recursive) + { + foreach (var subDir in dirs) + { + var targetDirectory = destinationDir.JoinDir(subDir.Name); + subDir.CopyTo(targetDirectory); + } + } + } + + public async Task CopyToAsync(DirectoryPath destinationDir, bool recursive = true) + { + ThrowIfNotExists(); + + // Cache directories before we start copying + var dirs = EnumerateDirectories().ToList(); + + destinationDir.Create(); + + // Get the files in the source directory and copy to the destination directory + foreach (var file in EnumerateFiles()) + { + var targetFilePath = destinationDir.JoinFile(file.Name); + await file.CopyToAsync(targetFilePath).ConfigureAwait(false); + } + + // If recursive and copying subdirectories, recursively call this method + if (recursive) + { + foreach (var subDir in dirs) + { + var targetDirectory = destinationDir.JoinDir(subDir.Name); + await subDir.CopyToAsync(targetDirectory).ConfigureAwait(false); + } + } + } + + /// + /// Move the directory to a destination path. + /// + public DirectoryPath MoveTo(DirectoryPath destinationDir) + { + Info.MoveTo(destinationDir.FullPath); + // Return the new path + return destinationDir; + } + + /// + /// Move the file to a target path. + /// + public async Task MoveToAsync(DirectoryPath destinationDir) + { + await Task.Run(() => Info.MoveTo(destinationDir.FullPath)).ConfigureAwait(false); + // Return the new path + return destinationDir; + } + + /// + /// Move the directory to a destination path as a subfolder with the current name. + /// + public async Task MoveToDirectoryAsync(DirectoryPath destinationParentDir) + { + await Task.Run(() => Info.MoveTo(destinationParentDir.JoinDir(Name))).ConfigureAwait(false); + // Return the new path + return destinationParentDir.JoinDir(this); + } + /// /// Join with other paths to form a new directory path. ///