Catch too long file paths

This topic contains 16 replies, has 11 voices, and was last updated by  frank jackson 1 year, 2 months ago.

  • Author
  • #11612

    Yegor Lopatin

    Hello guys
    Could You advise please. All know that .NET has problem with path bigger that 260 symbols. When i am seeking file i get that error. How could i log all long paths... somethink like

    $output = "C:\Users\user\$([string](Get-Date -Format dd.MM.yyyy)+".csv")"
    Remove-Item $output -ea SilentlyContinue
    $ErrorActionPreference = 'Stop'

    Get-ChildItem d:\tmp\ -Recurse | Where-Object {$_.length -gt 1022MB } |`
    select fullname,@{name='Size(MB)'; expression={[int]($_.Length/1MB)}}, CreationTime,LastWriteTime | export-csv $output
    Write-Warning "$_.Exception"

    but $_.Exception does not content bad Path

  • #11614

    Dave Wyatt

    Try/Catch only works when you're working with terminating errors, which would stop your whole pipeline. In this case, ditching try/catch and treating the errors as non-terminating is probably the better approach.

    Based on some tests, it looks like the error record contains the path of a directory which first contained something that was too long. If you want the full path to each file, you'd have to rewrite this code using the underlying Win32 API functions, which is a bit of a headache.

    Here's what's been working for me in tests so far:

    Get-ChildItem -Path c:\source\temp\*.txt -Recurse -ErrorAction SilentlyContinue -ErrorVariable err
    foreach ($errorRecord in $err)
        if ($errorRecord.Exception -is [System.IO.PathTooLongException])
            Write-Warning "Path too long in directory '$($errorRecord.TargetObject)'."
            Write-Error -ErrorRecord $errorRecord
  • #11615

    Vern Anderson

    In the beginning it looks like you have tried to write your own method for checking if a folder by that name already exist and is so delete it?
    I would try this instead.
    $output = "C:\Users\user\$([string](Get-Date -Format dd.MM.yyyy)+".csv")"
    $nameused = Test-Path $output
    if ($nameused -eq $True) { Remove-Item $output -Force -EA "SilentlyContinue"}

    I am not familiar with "$_.Exception" if you're trying to have your catch block print any warning messages that occurred in the try block you should include the -WarningVariable yegor and then. . .

    Write-Host $yegor

  • #11618

    Yegor Lopatin

    Dave, kindly thanks
    It works!
    Could You advise please resources where i can read about Errors, its properties, types.
    How did You know that needed property is TargetObject ? Magic)


  • #11619

    Dave Wyatt

    Well, in this case, I just created a file with a long path, ran Get-ChildItem, and then piped the resulting error object to Format-List * -Force. There, I saw that the TargetObject property had the information I was looking for, and I structured the rest of the code around that.

    Get-ChildItem -Path c:\source\temp\*.txt -Recurse
    $error[0] | Format-List * -Force

    I've done a lot of this sort of trial and error (no pun intended) to get a feel for PowerShell's error reporting capabilities and limitations. Unfortunately, each cmdlet is responsible for building meaningful ErrorRecord objects when they encounter an exception, and sometimes the information you get is really lacking (blank TargetObject property, etc.) In this case, though, it worked out okay.

    This blog post looks like a good starting point for the topic of PowerShell error handling. Wish it had been written when I started working on this stuff. 🙂

  • #11620


    I see this issue a lot so I created the following script that records each folder whose name is too long to a logfile in c:\outputfiles\longpaths.txt. I would assume it can be modified to also record filenames+paths by checking the "fullname"

    $destinationpath = "D:\"
    Write-Host "Start check"
    #Build a list of folders 
    $folders = Get-childitem $destinationpath -recurse | where {$_.mode -like "d----"}  
    Foreach ($folder in $folders){ 
            $dump = Get-ChildItem $folder.FullName -ErrorAction Stop 
        $FolderFullName = $folder.fullname 
            Add-Content -Value "$folderFullname" -Path "c:\outputfiles\LongPaths.txt"
    Write-Host "Check Done"
  • #11621

    Yegor Lopatin

    When $destinationpath is around 30 TB of data
    "Build a list of folders" as alone proccess is not a good idea + if you use RDP session
    but thank You for srcript

  • #18434

    Snuff Six

    Hi, this WILL help you.
    Get AlphaFS.dll:



    [Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $True)

  • #21256

    JohnRock Bilodeau

    I'm trying to get around the path too long error and was planing on trying AlphaFS, but can't seem to get it to work....or at least in anything above PowerShell v2.
    If I try on v3 or 4 i simply get an error

    Import-Module : Could not load file or assembly 'file:///C:\AlphaFS\AlphaFS.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

    any idea's on how to get this to work with newer versions of powershell?

  • #31748

    Karin Brobras

    you are trying alphaFS but it seems not working even version 3 or 4 get error, in this situation Path Long Tool may be useful many forums also refer to this program.

  • #31791

    Mark Hammonds

    Read this article I used it in my search for long paths it works well

  • #37569


    Please try long path tool !

  • #67662

    frank jackson

    Long Path Tool can scan for any duplicates and deletes them automatically and fix all of your problems.

    • #69021

      frank jackson

      In other words search google for Long Path Tool and it will resolve all of your problems

  • #11616

    Yegor Lopatin

    Thanks a lot, guys

    The error is like
    Get-ChildItem : 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
    At C:\Users\user\Desktop\Get-BigFiles+.ps1:4 char:17
    + Get-ChildItem < <<< d:\ftp\ -Recurse | Where-Object {$_.length -gt 1022MB } |` + CategoryInfo : ReadError: (D:\ftp\files\Ca... :String) [Get-ChildItem], PathTooLongException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand >>If you want the full path to each file, you’d have to rewrite this code using the underlying Win32 API functions, which is a bit of a headache.
    it seems, i will leave this idea of logging long paths)))

    thanks for responses

  • #11617

    Dave Wyatt

    Try the code I posted. It doesn't give you the complete path of every file, but it does at least give you an idea of where to start looking (the highest folder in that path that was still legal, according to .NET.) The information you want isn't in the Exception, it's in the TargetObject property of the error record.

You must be logged in to reply to this topic.