Created
May 14, 2020 16:36
-
-
Save maxfridbe/de1a28806a55b443f6692b796fd09b50 to your computer and use it in GitHub Desktop.
..fucking powershell
This file contains hidden or 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
$id = get-random | |
$maxSizeMB = 500 | |
$code = @" | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Collections.Generic; | |
namespace HelloWorld | |
{ | |
public class Program$id | |
{ | |
public static List<string> ListDirsToDel() | |
{ | |
var todel = new List<string>(); | |
var dirs = Directory.EnumerateDirectories("."); | |
foreach (var d in dirs){ | |
var size = DirSize(new DirectoryInfo(d)); | |
var sizemb = size/1024f/1024f; | |
Console.WriteLine( "Size: "+sizemb + "mb \t\tDir "+d); | |
if($maxSizeMB > sizemb) | |
{ | |
todel.Add(d); | |
} | |
} | |
return todel; | |
} | |
public static long DirSize(DirectoryInfo dir) | |
{ | |
return dir.GetFiles().Sum(fi => fi.Length) + | |
dir.GetDirectories().Sum(di => DirSize(di)); | |
} | |
} | |
} | |
"@ | |
Add-Type -TypeDefinition $code -Language CSharp | |
$data = iex "[HelloWorld.Program$id]::ListDirsToDel()" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment