Skip to content

Instantly share code, notes, and snippets.

@cryophobia
Created September 7, 2016 08:06
Show Gist options
  • Save cryophobia/70c696da061d393f8eda51e11b86c290 to your computer and use it in GitHub Desktop.
Save cryophobia/70c696da061d393f8eda51e11b86c290 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace ObliterateFolders {
class MainClass {
public static void Main(string[] args) {
TraverseTree(args[0]);
Console.WriteLine("Press any key");
Console.ReadKey();
}
public static void TraverseTree(string root) {
Stack<string> dirs = new Stack<string>(20);
if (!System.IO.Directory.Exists(root)) {
throw new ArgumentException();
}
dirs.Push(root);
while (dirs.Count > 0) {
string currentDir = dirs.Pop();
string[] subDirs;
try {
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
catch (UnauthorizedAccessException e) {
Console.WriteLine(e.Message);
continue;
} catch (System.IO.DirectoryNotFoundException e) {
Console.WriteLine(e.Message);
continue;
}
foreach (string str in subDirs) {
if (str.EndsWith("obj", StringComparison.Ordinal)) {
Console.WriteLine(str);
System.IO.Directory.Delete(str, true);
}
if (str.EndsWith("bin", StringComparison.Ordinal)) {
Console.WriteLine(str);
System.IO.Directory.Delete(str, true);
}
dirs.Push(str);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment