The specified path, file name, or both are too long

Hi

Hoping someone can help, I've been running a script to delete files/folders and it's been running great, however recently some of the folder names have become very long and the script cannot delete these and errors with:

"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 googled this error and it seems that this is a limitation of PS and .Net?
Is there any way around this?

Here is the code for the script:
$filelocation = "e:\oldbackups"

 
gci -path $filelocation -r | where-object {$_.PsIsContainer -eq "True"} |
    ?{$_.creationtime -lt (get-date).adddays(-30)} | %{del $_.fullname -r -force}

  • Maybe a recursive function would help? Something similar to my answer at the bottom of http://powershell.com/cs/forums/t/6192.aspx. That post is for a totally different problem, but the recursion is the same.

     

  • Thanks for your reply and help Patrick.

    I'm very green to PS and to be honest I really hacked together that script I was using which worked well until the folders changed into longer names.

    Not sure where I see the recursion that I need to use in your script, is it the child path component?

    [System.IO.DirectoryInfo]$di = New-Object System.IO.DirectoryInfo($childPath)
                    if (($di.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne [System.IO.FileAttributes]::ReparsePoint)
                    {
                        Find-File $childPath

    Thanks again.

  • Yes, it's the part you indicated. Here's a slightly simplified example, with a couple of comments. 

    function Find-File([string]$folderPath)
    {
        if (Test-Path $folderPath)
        {
            try
            {
                [string[]]$childFolderPaths = [System.IO.Directory]::GetDirectories($folderPath)
                foreach($childPath in $childFolderPaths)
                {
                    [System.IO.DirectoryInfo]$di = New-Object System.IO.DirectoryInfo($childPath)
                    if (($di.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne [System.IO.FileAttributes]::ReparsePoint)
                    {
                   
                        <#
                        This is the recursive part. That is, this function calls itself in order to process child directories.
                        #>
                   
                        Find-File $childPath
                    }
                }
                [string[]]$filePaths = [System.IO.Directory]::GetFiles($folderPath, "*.*", [System.IO.SearchOption]::TopDirectoryOnly)
                foreach($filePath in $filePaths)
                {
               
                    <#
                   
                    Here is where you'd take whatever action you want. I'm looking for files named AssemblyInfo.cs and displaying their full paths.
                   
                    #>
               
                    if (([System.IO.Path]::GetFileName($filePath)) -ieq "AssemblyInfo.cs")
                    {
                        Write-Host $filePath -ForegroundColor DarkGreen
                    }
                }
            }
            catch
            {
                Write-Host ($Error[0]) -ForegroundColor Red
            }
        }
    }


    Find-File "C:\Work"

  • I have found this software for the problem you are facing right now.
    It is highly recommended that you should try the following software.
    I used this software and it is very user friendly to copy the long path.

     LongPathTool.com