Last active
August 23, 2016 20:03
-
-
Save abergs/e682cd1f98f5170e86a6 to your computer and use it in GitHub Desktop.
Remove node_modules
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
# 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 |
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.
@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
Usage
.\rdel.ps1 .\node_modules
from powershell