Author Posts

November 22, 2013 at 2:40 am

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'

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

but $_.Exception does not content bad Path

November 22, 2013 at 6:39 am

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)'."
    }
    else
    {
        Write-Error -ErrorRecord $errorRecord
    }
}

November 22, 2013 at 6:47 am

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. . .

catch{
Write-Host $yegor
}

November 22, 2013 at 6:53 am

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

November 22, 2013 at 6:55 am

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.

November 22, 2013 at 7:07 am

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)

Thanks

November 22, 2013 at 7:17 am

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. 🙂

November 22, 2013 at 8:09 am

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){ 
    Try{ 
        $dump = Get-ChildItem $folder.FullName -ErrorAction Stop 
        } 
    Catch{ 
    $FolderFullName = $folder.fullname 
        Add-Content -Value "$folderFullname" -Path "c:\outputfiles\LongPaths.txt"
        } 
} 
cls      
Write-Host "Check Done"

November 22, 2013 at 8:16 am

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

September 2, 2014 at 5:58 am

Hi, this WILL help you.
Get AlphaFS.dll: https://alphafs.codeplex.com/SourceControl/list/changesets

PowerShell:

Import-Module

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

December 16, 2014 at 12:31 pm

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?

January 21, 2015 at 7:17 am

[blockquote]Get AlphaFS.dll: https://alphafs.codeplex.com/SourceControl/list/changesets
PowerShell:
Import-Module
[Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $True)[/blockquote]
OK, and filtering of e.g. lastaccessdate – how to do this?

November 6, 2015 at 10:41 am

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.

April 12, 2016 at 7:09 am

Please try long path tool !

March 31, 2017 at 10:24 am

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

April 20, 2017 at 10:20 am

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