Last active
October 28, 2021 12:23
-
-
Save srndpty/43c76d74188884f25716f25a44ef0818 to your computer and use it in GitHub Desktop.
Zap all files from all subdirectories recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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