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
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
}
}
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. . .
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
>>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)))
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.
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)
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.
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.
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"
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
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?
[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?
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.