Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.NET build output cleanup script
#time
open System.IO
open System.Globalization
let EnumerateDirectories path =
Directory.EnumerateDirectories(path) |> Seq.toList
let isObjOrBinFolder (folderName:string) =
folderName.EndsWith("obj", true, CultureInfo.InvariantCulture) || folderName.EndsWith("bin", true, CultureInfo.InvariantCulture)
let rec getFoldersToDelete path =
match EnumerateDirectories path with
| [] -> []
| subfolders ->
let targetFolders = subfolders
|> List.filter isObjOrBinFolder
let targets = subfolders
|> List.filter (isObjOrBinFolder >> not)
|> List.collect getFoldersToDelete
|> List.append targetFolders
targets
let deleteFoldersAndSubFolders path =
getFoldersToDelete path
|> List.iter (fun dir ->
printfn "Deleting: %s" dir
Directory.Delete dir)
let ensureDirectoryExists directory =
if Directory.Exists directory then
Some directory
else
printfn "Directory %s not found and will be skipped" directory
None
match fsi.CommandLineArgs |> Array.skip 1 with
| [||] ->
deleteFoldersAndSubFolders (Directory.GetCurrentDirectory())
| directoryPaths ->
directoryPaths
|> Array.map ensureDirectoryExists
|> Array.choose id
|> Array.iter deleteFoldersAndSubFolders
// let path = "/Users/mallibone/Downloads/Testground/test"
let defaultPath = Directory.GetCurrentDirectory()
match fsi.CommandLineArgs |> Array.skip 1 with
| [||] ->
deleteFoldersAndSubFolders defaultPath
| [|"--help"|] ->
printfn "Usage of the BuildDirCleanup script"
printfn ""
printfn "dotnet fsi BuildDirCleanup.fsx [dir1 dir2 .. dirn]"
printfn ""
printfn "In every directory provided and it's subdirectories the bin and obj folders will be deleted."
printfn "If no directory is given the current direcotry (%s)" defaultPath
printfn "will be recursivley searched."
printfn ""
| directoryPaths ->
directoryPaths
|> Array.map ensureDirectoryExists
|> Array.choose id
|> Array.iter deleteFoldersAndSubFolders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment