diff --git a/Pathy.ApiVerificationTests/ApprovedApi/pathy.net47.verified.txt b/Pathy.ApiVerificationTests/ApprovedApi/pathy.net47.verified.txt index b27fea0..870cd4c 100644 --- a/Pathy.ApiVerificationTests/ApprovedApi/pathy.net47.verified.txt +++ b/Pathy.ApiVerificationTests/ApprovedApi/pathy.net47.verified.txt @@ -41,6 +41,8 @@ namespace Pathy { public static void CreateDirectoryRecursively(this Pathy.ChainablePath path) { } public static void DeleteFileOrDirectory(this Pathy.ChainablePath path) { } + public static void DeleteFileOrDirectory(this System.Collections.Generic.IEnumerable paths) { } + public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable sourcePaths, Pathy.ChainablePath destinationDirectory) { } public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { } } public static class StringExtensions diff --git a/Pathy.ApiVerificationTests/ApprovedApi/pathy.net8.0.verified.txt b/Pathy.ApiVerificationTests/ApprovedApi/pathy.net8.0.verified.txt index b6b2481..c6a581a 100644 --- a/Pathy.ApiVerificationTests/ApprovedApi/pathy.net8.0.verified.txt +++ b/Pathy.ApiVerificationTests/ApprovedApi/pathy.net8.0.verified.txt @@ -42,6 +42,8 @@ namespace Pathy { public static void CreateDirectoryRecursively(this Pathy.ChainablePath path) { } public static void DeleteFileOrDirectory(this Pathy.ChainablePath path) { } + public static void DeleteFileOrDirectory(this System.Collections.Generic.IEnumerable paths) { } + public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable sourcePaths, Pathy.ChainablePath destinationDirectory) { } public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { } } public static class StringExtensions diff --git a/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.0.verified.txt b/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.0.verified.txt index b27fea0..870cd4c 100644 --- a/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.0.verified.txt +++ b/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.0.verified.txt @@ -41,6 +41,8 @@ namespace Pathy { public static void CreateDirectoryRecursively(this Pathy.ChainablePath path) { } public static void DeleteFileOrDirectory(this Pathy.ChainablePath path) { } + public static void DeleteFileOrDirectory(this System.Collections.Generic.IEnumerable paths) { } + public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable sourcePaths, Pathy.ChainablePath destinationDirectory) { } public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { } } public static class StringExtensions diff --git a/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.1.verified.txt b/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.1.verified.txt index b6b2481..c6a581a 100644 --- a/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.1.verified.txt +++ b/Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.1.verified.txt @@ -42,6 +42,8 @@ namespace Pathy { public static void CreateDirectoryRecursively(this Pathy.ChainablePath path) { } public static void DeleteFileOrDirectory(this Pathy.ChainablePath path) { } + public static void DeleteFileOrDirectory(this System.Collections.Generic.IEnumerable paths) { } + public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable sourcePaths, Pathy.ChainablePath destinationDirectory) { } public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { } } public static class StringExtensions diff --git a/Pathy.Specs/ChainablePathExtensionSpecs.cs b/Pathy.Specs/ChainablePathExtensionSpecs.cs index 48eec54..f0a4341 100644 --- a/Pathy.Specs/ChainablePathExtensionSpecs.cs +++ b/Pathy.Specs/ChainablePathExtensionSpecs.cs @@ -129,4 +129,100 @@ public void Can_move_a_directory_under_another_using_a_new_name() // Assert (testFolder / "Destination" / "NewName" / "temp.txt").Exists.Should().BeTrue(); } + + [Fact] + public void Can_delete_multiple_files() + { + // Arrange + var file1 = testFolder / "file1.txt"; + var file2 = testFolder / "file2.txt"; + var file3 = testFolder / "file3.txt"; + File.WriteAllText(file1, "Hello World!"); + File.WriteAllText(file2, "Hello World!"); + File.WriteAllText(file3, "Hello World!"); + + var files = new[] { file1, file2, file3 }; + + // Act + files.DeleteFileOrDirectory(); + + // Assert + file1.FileExists.Should().BeFalse(); + file2.FileExists.Should().BeFalse(); + file3.FileExists.Should().BeFalse(); + } + + [Fact] + public void Can_delete_multiple_directories() + { + // Arrange + var dir1 = testFolder / "dir1"; + var dir2 = testFolder / "dir2"; + var dir3 = testFolder / "dir3"; + dir1.CreateDirectoryRecursively(); + dir2.CreateDirectoryRecursively(); + dir3.CreateDirectoryRecursively(); + File.WriteAllText(dir1 / "file.txt", "Hello World!"); + File.WriteAllText(dir2 / "file.txt", "Hello World!"); + File.WriteAllText(dir3 / "file.txt", "Hello World!"); + + var directories = new[] { dir1, dir2, dir3 }; + + // Act + directories.DeleteFileOrDirectory(); + + // Assert + dir1.Exists.Should().BeFalse(); + dir2.Exists.Should().BeFalse(); + dir3.Exists.Should().BeFalse(); + } + + [Fact] + public void Can_move_multiple_files_to_a_directory() + { + // Arrange + (testFolder / "Source").CreateDirectoryRecursively(); + (testFolder / "Destination").CreateDirectoryRecursively(); + var file1 = testFolder / "Source" / "file1.txt"; + var file2 = testFolder / "Source" / "file2.txt"; + var file3 = testFolder / "Source" / "file3.txt"; + File.WriteAllText(file1, "Hello World!"); + File.WriteAllText(file2, "Hello World!"); + File.WriteAllText(file3, "Hello World!"); + + var files = new[] { file1, file2, file3 }; + + // Act + files.MoveFileOrDirectory(testFolder / "Destination"); + + // Assert + file1.Exists.Should().BeFalse(); + file2.Exists.Should().BeFalse(); + file3.Exists.Should().BeFalse(); + (testFolder / "Destination" / "file1.txt").Exists.Should().BeTrue(); + (testFolder / "Destination" / "file2.txt").Exists.Should().BeTrue(); + (testFolder / "Destination" / "file3.txt").Exists.Should().BeTrue(); + } + + [Fact] + public void Can_move_multiple_directories_under_another_directory() + { + // Arrange + (testFolder / "Source" / "dir1").CreateDirectoryRecursively(); + (testFolder / "Source" / "dir2").CreateDirectoryRecursively(); + (testFolder / "Destination").CreateDirectoryRecursively(); + File.WriteAllText(testFolder / "Source" / "dir1" / "file.txt", "Hello World!"); + File.WriteAllText(testFolder / "Source" / "dir2" / "file.txt", "Hello World!"); + + var directories = new[] { testFolder / "Source" / "dir1", testFolder / "Source" / "dir2" }; + + // Act + directories.MoveFileOrDirectory(testFolder / "Destination"); + + // Assert + (testFolder / "Source" / "dir1").Exists.Should().BeFalse(); + (testFolder / "Source" / "dir2").Exists.Should().BeFalse(); + (testFolder / "Destination" / "dir1" / "file.txt").Exists.Should().BeTrue(); + (testFolder / "Destination" / "dir2" / "file.txt").Exists.Should().BeTrue(); + } } diff --git a/Pathy/ChainablePathExtensions.cs b/Pathy/ChainablePathExtensions.cs index 7846498..ef9065c 100644 --- a/Pathy/ChainablePathExtensions.cs +++ b/Pathy/ChainablePathExtensions.cs @@ -61,4 +61,30 @@ public static void MoveFileOrDirectory(this ChainablePath sourcePath, ChainableP Directory.Move(sourcePath, Path.Combine(destinationDirectory.ToString(), newName ?? sourcePath.Name)); } } + + /// + /// Deletes the files or directories represented by the specified collection of instances. + /// If a path represents a directory, it is deleted recursively. + /// + public static void DeleteFileOrDirectory(this System.Collections.Generic.IEnumerable paths) + { + foreach (var path in paths) + { + path.DeleteFileOrDirectory(); + } + } + + /// + /// Moves files or directories represented by the specified collection of instances to a target directory. + /// + /// The collection of paths of the files or directories to move. + /// The target directory where the files or directories will be moved. + public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable sourcePaths, + ChainablePath destinationDirectory) + { + foreach (var sourcePath in sourcePaths) + { + sourcePath.MoveFileOrDirectory(destinationDirectory); + } + } } diff --git a/README.md b/README.md index 8b086c0..76b0317 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,22 @@ Next to that, Pathy also provides a bunch of extension methods to operate on the * `DeleteFileOrDirectory` * `MoveFileOrDirectory` +These methods also support operating on collections of `ChainablePath` objects: + +```csharp +// Delete multiple files or directories at once +var files = new[] { + ChainablePath.Temp / "file1.txt", + ChainablePath.Temp / "file2.txt", + ChainablePath.Temp / "dir1" +}; +files.DeleteFileOrDirectory(); + +// Move multiple files to a destination directory +var filesToMove = (ChainablePath.Current / "source").GlobFiles("*.txt"); +filesToMove.MoveFileOrDirectory(ChainablePath.Current / "destination"); +``` + ## Building To build this repository locally so you can contribute to it, you need the following: