Skip to content

Instantly share code, notes, and snippets.

@srndpty
Last active October 28, 2021 12:23
Show Gist options
  • Save srndpty/43c76d74188884f25716f25a44ef0818 to your computer and use it in GitHub Desktop.
Save srndpty/43c76d74188884f25716f25a44ef0818 to your computer and use it in GitHub Desktop.
Zap all files from all subdirectories recursively
using System.IO;
namespace ZapAll
{
class Program
{
private static readonly char separator = Path.DirectorySeparatorChar;
static void Main(string[] args)
{
foreach (var item in Directory.EnumerateDirectories(Directory.GetCurrentDirectory()))
{
Zap(item);
}
}
static void Zap(string path)
{
// process children directories first
foreach (var dir in Directory.EnumerateDirectories(path))
{
Zap(dir);
}
// zap all files
foreach (var oldPath in Directory.EnumerateFiles(path))
{
var newPath = oldPath.Insert(oldPath.LastIndexOf(separator), "_");
newPath = newPath.Remove(newPath.LastIndexOf(separator), 1);
File.Move(oldPath, newPath);
}
// delete directory
Directory.Delete(path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment