Last active
March 22, 2021 22:51
-
-
Save joanjane/1d08b710edbfcfb66ee32e85e5e4d74c to your computer and use it in GitHub Desktop.
Powershell script to clean development packages and binaries recursively (node_modules, nuget packages...)
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
# WARNING: | |
# IF YOU ARE NOT SURE OF WHAT THIS SCRIPT DOES, DO A BACKUP FIRST. | |
# THIS CANNOT BE UNDONE, SO BE AWARE OF ANY POSSIBLE LOST FILES. | |
# Run this on a safe folder where you know the contents, DO NOT RUN THIS IN A FOLDER LIKE "Program Files"! | |
# I take no responsability of any lost files or damages related to this script. | |
# | |
# Instructions: | |
# This script will delete bin, obj, node_modules and packages folders recursively from the current folder. You can specify the path and folders to delete with -path and -folderNames options. | |
# Options: | |
# -path (Optional): The path to delete subfolders recursively, by default the current folder. | |
# -folderNames (Optional): Folder names to match recursively under $path parameter. By default (bin,obj,node_modules,packages). | |
# -listMode (Optional): To test which files will be deleted, use -listMode flag to list the folders without deleting. | |
# -confirm (Optional): Run delete script without confirm prompt | |
param( | |
[string]$path = ".\", | |
[string[]] $folderNames = @('bin', 'obj', 'node_modules', 'packages', '.vs', '.gradle'), | |
[switch]$confirm = $false, | |
[switch]$listMode = $false) | |
function ListFolders([string]$path, [string[]] $folderNames) { | |
Get-ChildItem $path -include $folderNames -Recurse -Directory -Force | |
} | |
function Confirm($message, $confirm) { | |
if ($confirm -eq $false) { | |
$confirmation = Read-Host "$message [y/n]" | |
while ($confirmation -ne "y") { | |
if ($confirmation -eq 'n') { exit } | |
$confirmation = Read-Host $message | |
} | |
} | |
} | |
if ($listMode -eq $false) { | |
Confirm -message "Are you sure you want to delete $folderNames folders recursively from the current folder?" -confirm:$confirm | |
} | |
ListFolders $path $folderNames | foreach ($_) { | |
if ($listMode -eq $true) { | |
Write-Host $_.fullname | |
} else { | |
Write-Host "Deleting '$($_.fullname)'" | |
Remove-Item $_.fullname -Force -Recurse | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment