Skip to content

Instantly share code, notes, and snippets.

@abergs
Last active August 23, 2016 20:03
Show Gist options
  • Save abergs/e682cd1f98f5170e86a6 to your computer and use it in GitHub Desktop.
Save abergs/e682cd1f98f5170e86a6 to your computer and use it in GitHub Desktop.
Remove node_modules
# By Anders and Henkan 2014-08-07 v1
# http://ideasof.andersaberg.com
$path = $args[0]
$chars = "abcdefghijklmnopqrstuvwxyz"
function GetFileName($num)
{
$name = ""
while($num -gt 25) {
$name += "z"
$num -= 26
}
$name += $chars[$num]
return $name
}
function RenameRec($xpath) {
$counter = -1
$items = @(Get-ChildItem $xpath | ForEach-Object -Process {$_.FullName})
foreach($item in $items) {
$counter += 1
$filename = split-path $item -Leaf
$name = GetFileName($counter)
#write-host "OLD $filename NEW $name"
if($filename -ne $name){
#Write-Host "Renaming $item to $name"
Rename-Item $item $name
}
}
$folders = @(Get-ChildItem $xpath -Directory | ForEach-Object -Process {$_.FullName})
foreach($folder in $folders) {
RenameRec($folder)
}
}
Write-Host "Renaming all files... (this can take a while, Ignore any errors)"
RenameRec($path)
Remove-Item $path
@abergs
Copy link
Author

abergs commented Aug 7, 2014

Usage

  • Copy-paste into new file rdel.ps1
  • Run .\rdel.ps1 .\node_modules from powershell

@joezimjs
Copy link

Instead of renaming files, you can just use a built in command that is able to work around the path being too long: robocopy. The following commands will create an empty directory, then use robocopy to "mirror" that directory's contents into the destination directory (e.g. node_modules) which will delete all of the files in the destination. Than you just delete the empty folder and the destination folder. I put this in a rmd.cmd file in my path:

@echo off

mkdir empty
robocopy empty %* /mir
rm empty -r
rm %* -r

Depending on the depth of the tree, your script can still fail and it isn't usable on the normal command prompt.

@haldiggs
Copy link

haldiggs commented Nov 3, 2015

@joezimjs thanks, works great. No need to use powershell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment