Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete directory regardless of 260 char limit

I'm writing a simple script to delete USMT migration folders after a certain amount of days:

## Server List ## $servers = "Delorean","Adelaide","Brisbane","Melbourne","Newcastle","Perth"  ## Number of days (-3 is over three days ago) ## $days = -3  $timelimit = (Get-Date).AddDays($days)  foreach ($server in $servers) {     $deletedusers = @()     $folders = Get-ChildItem \\$server\USMT$ | where {$_.psiscontainer}     write-host "Checking server : " $server     foreach ($folder in $folders)      {         If ($folder.LastWriteTime -lt $timelimit -And $folder -ne $null)         {             $deletedusers += $folder             Remove-Item -recurse -force $folder.fullname         }     }         write-host "Users deleted : " $deletedusers         write-host } 

However I keep hitting the dreaded 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.

I've been looking at workarounds and alternatives but they all revolve around me caring what is in the folder.

I was hoping for a more simple solution as I don't really care about the folder contents if it is marked for deletion.

Is there any native Powershell cmdlet other than Remove-Item -recurse that can accomplish what I'm after?

like image 402
mhouston100 Avatar asked May 06 '13 05:05

mhouston100


People also ask

How do I delete a folder name that is too long?

You have to navigate to the folder, and THEN use shift + Del like @cori said. If you select the Delete option from the context menu, it WILl fail there. shift + Del it!

How do I delete a folder with millions of files?

Navigate to the folder that you want to delete (with all its files and subfolders). Use cd *path*, for example, cd C:\Trash\Files\ to do so. Use cd .. to navigate to the parent folder and run the command RMDIR /Q/S *foldername* to delete the folder and all of its subfolders.

How do I delete a long filename in PowerShell?

With PowerShell 7.0, there is no need to use the Unicode version of the path because it already has built-in support for long path names. If the folders need to be deleted, too, just remove the -File parameter from the Get-ChildItem cmdlet, and PowerShell should delete all items, including files and folders.

How do I delete a large directory in Linux?

Sometimes, find $DIR_TO_DELETE -type f -delete is faster than rm -rf . You may also want to try out mkdir /tmp/empty && rsync -r --delete /tmp/empty/ $DIR_TO_DELETE . Finally, if you need to delete the content of a whole partition, the fastest will probably be umount , mkfs and re- mount .


2 Answers

I often have this issue with node projects. They nest their dependencies and once git cloned, it's difficult to delete them. A nice node utility I came across is rimraf.

npm install rimraf -g rimraf <dir> 
like image 132
Andrew Chaa Avatar answered Sep 30 '22 06:09

Andrew Chaa


Just as CADII said in another answer: Robocopy is able to create paths longer than the limit of 260 characters. Robocopy is also able to delete such paths. You can just mirror some empty folder over your path containing too long names in case you want to delete it.

For example:

robocopy C:\temp\some_empty_dir E:\temp\dir_containing_very_deep_structures /MIR 

Here's the Robocopy reference to know the parameters and various options.

like image 28
Harald Avatar answered Sep 30 '22 07:09

Harald