Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Forked from manuelsidler/16_exercise.fsx
Created July 2, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/a76102f7ad67107561320fc4d9ca58c2 to your computer and use it in GitHub Desktop.
Save isaacabraham/a76102f7ad67107561320fc4d9ca58c2 to your computer and use it in GitHub Desktop.
Sort subfolders by folder size in F#
open System.IO
type FolderStatistic = { Name : string
Size : int64 }
let calculateFolderSize (folder : string) =
folder
|> Directory.GetFiles
|> Array.sumBy (fun file -> (FileInfo file).Length)
let getSubfolders path =
let folder = DirectoryInfo path
folder.GetDirectories("*", SearchOption.AllDirectories)
getSubfolders @"C:\Users\Isaac\Source\Repos\safe-test"
|> Array.map (fun x -> { Name = x.FullName; Size = calculateFolderSize x.FullName })
|> Array.sortByDescending(fun x -> x.Size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment