-1

This is frustrating me so i am asking you guys for help..

i have a million(exaggerating a bit) folders to delete, I can go one by one and delete from Windows Explorer YAY... BUT I want to script it in Powershell (version 4)

using this code:

$Path = '\\verylonguncpath\plussomemore\'
Remove-Item -Path $Path -Recurse -Force -Confirm:$False

I get the error:

Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. At line:5 char:1 + Remove-Item -Path $Path -Recurse -Force -Confirm:$False + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (\\verylongpath\plussomemore:String) [Remove-Item], PathTooLongException + FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

It works if the $Path isn't very long....

I even tried mapping to a letter then deleting content but i get the same problem?

Apparently its Microsoft or Powershell that is the problem from what i having been reading?

maybe its a user error :\

any advice would be great, i would really like to just use MS products but third party programs will be fine (to use within Powershell script)..

Thanks Pav.

Pavle Stojanovic
  • 525
  • 2
  • 8
  • 30
  • These very long paths are handled by Explorer correctly? – Etan Reisner Feb 03 '15 at 23:16
  • 3
    I think this is more or less a duplicate question. See here: [http://stackoverflow.com/questions/16392765/powershell-delete-directory-regardless-of-260-char-limit][1] [1]: http://stackoverflow.com/questions/16392765/powershell-delete-directory-regardless-of-260-char-limit – campbell.rw Feb 04 '15 at 00:03

3 Answers3

1
function Remove-FolderDeep([string]$folder){
$SubFolderList = Get-ChildItem -Path $folder -Directory -ErrorAction Ignore
for ($i = 0; $i -lt $SubFolderList.Length; $i++) {
    # rename the subfolder to avoid long file name
    Rename-Item -Path ($folder + "\" + $SubFolderList[$i]) -NewName ($folder + "\" + $i)
    Remove-FolderDeep -folder ($folder + "\" + $i)
}
# remove the folder 
Remove-Item $folder -Force -Recurse

}

Tanvir Ather
  • 856
  • 7
  • 4
0

This works:

Cmd /C "rmdir /S /Q $myDir"

Just make sure you create your variable using quotes and run the above command.

$mydir = "\pathToDelete\"
TheEsnSiavashi
  • 1,245
  • 1
  • 14
  • 29
Lelouch
  • 549
  • 6
  • 6
-2

@campbell.rw

Thank you!

NOW I can delete these folders with ease :) YAY

Pavle gives you 51 fake internet points

So the answer is below and it works...well for me it did.

Delete directory regardless of 260 char limit

Cmd /C "rmdir /S /Q $myDir"

Community
  • 1
  • 1
Pavle Stojanovic
  • 525
  • 2
  • 8
  • 30
  • A number of people (now me included) have sadly foudn that does not work for all cases. – rob Mar 17 '15 at 10:01
  • If the folder has a space it will stop... Their moght be a better way of doing it but i do this....$path = """$path""" it works because you entering the path in quotation marks. Let me know how it goes. – Pavle Stojanovic Mar 19 '15 at 02:57
  • tried it and it's unable to delete the whole node-modules folder – guillem Feb 09 '17 at 14:26