Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pathy.ChainablePath> paths) { }
public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable<Pathy.ChainablePath> sourcePaths, Pathy.ChainablePath destinationDirectory) { }
public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { }
}
public static class StringExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pathy.ChainablePath> paths) { }
public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable<Pathy.ChainablePath> sourcePaths, Pathy.ChainablePath destinationDirectory) { }
public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { }
}
public static class StringExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pathy.ChainablePath> paths) { }
public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable<Pathy.ChainablePath> sourcePaths, Pathy.ChainablePath destinationDirectory) { }
public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { }
}
public static class StringExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pathy.ChainablePath> paths) { }
public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable<Pathy.ChainablePath> sourcePaths, Pathy.ChainablePath destinationDirectory) { }
public static void MoveFileOrDirectory(this Pathy.ChainablePath sourcePath, Pathy.ChainablePath destinationDirectory, string newName = null) { }
}
public static class StringExtensions
Expand Down
96 changes: 96 additions & 0 deletions Pathy.Specs/ChainablePathExtensionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
26 changes: 26 additions & 0 deletions Pathy/ChainablePathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,30 @@ public static void MoveFileOrDirectory(this ChainablePath sourcePath, ChainableP
Directory.Move(sourcePath, Path.Combine(destinationDirectory.ToString(), newName ?? sourcePath.Name));
}
}

/// <summary>
/// Deletes the files or directories represented by the specified collection of <see cref="ChainablePath"/> instances.
/// If a path represents a directory, it is deleted recursively.
/// </summary>
public static void DeleteFileOrDirectory(this System.Collections.Generic.IEnumerable<ChainablePath> paths)
{
foreach (var path in paths)
{
path.DeleteFileOrDirectory();
}
}

/// <summary>
/// Moves files or directories represented by the specified collection of <see cref="ChainablePath"/> instances to a target directory.
/// </summary>
/// <param name="sourcePaths">The collection of paths of the files or directories to move.</param>
/// <param name="destinationDirectory">The target directory where the files or directories will be moved.</param>
public static void MoveFileOrDirectory(this System.Collections.Generic.IEnumerable<ChainablePath> sourcePaths,
ChainablePath destinationDirectory)
{
foreach (var sourcePath in sourcePaths)
{
sourcePath.MoveFileOrDirectory(destinationDirectory);
}
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down