Working with Long File Names that exceed the limit.

trying to output file names that exceed the limit to a document it runs through all the files with no errors but it looks like it skips the long files. I read some where you need to create a PS drive in order to have poweshell get-item that exceeds the length. I know it looks bad

#requires -Version 3
Set-StrictMode -Version Latest
set-location $env:USERPROFILE
[int]$maxpathlength = 220
$profileRoot = $env:USERPROFILE
$paths =  ("Documents" , "Desktop")

if(!(test-path c:\FLDRRDR)){Md c:\FLDRRDR}
foreach($path in $paths){
    if(test-path ("$profileRoot" + "\$path")){
        Try {
            $files = Get-ChildItem -Path $path -Recurse -ErrorAction Stop
            Foreach($file in $files){
                if($file.FullName.Length -gt $maxpathlength) {
                    $file.FullName | Out-File c:\FLDRRDR\Long-File-Names-Log.txt -Append
                }
            } 
        }        
        Catch{
            set-location -Path  ("$profileRoot" + "\$path")
            $root = Get-ChildItem -Directory
            foreach($subfolder in $root){
                set-location -Path  ("$profileRoot" + "\$path" + "\$subfolder")
                new-psdrive -Name X -PSProvider FileSystem -Root ("$profileRoot" + "\$path" + "\$subfolder")
                x:
                    Try {
                        $files = Get-ChildItem -Path x: -Recurse -ErrorAction Stop 
                        Foreach($file in $files){
                            if($file.FullName.Length -gt $maxpathlength) {
                                $file.FullName | Out-File c:\FLDRRDR\Long-File-Names-Log.txt -Append
                            }
                        }
                        set-location -Path  ("$profileRoot" + "\$path" + "\$subfolder")
                        remove-psdrive -name x
                    }        
                    Catch{
                        set-location -Path  ("$profileRoot" + "\$path" + "\$subfolder" + "\$subfolder2")
                        remove-psdrive -name x
                        $root2 = Get-ChildItem -Directory
                        foreach($subfolder2 in $root2){
                            new-psdrive -Name X -PSProvider FileSystem -Root ("$profileRoot" + "\$path" + "\$subfolder" + "\$subfolder2")
                            x:
                                Try {
                                    $files = Get-ChildItem -Path x: -Recurse -ErrorAction Stop
                                    Foreach($file in $files){
                                        if($file.FullName.Length -gt $maxpathlength) {
                                            $file.FullName | Out-File c:\FLDRRDR\Long-File-Names-Log.txt -Append                       
                                        }
                                    }
                                    set-location -Path  ("$profileRoot" + "\$path" + "\$subfolder" + "\$subfolder2")
                                    remove-psdrive -name x
                                }        
                                Catch{
                                "OUCH!"
                                set-location -Path  ("$profileRoot" + "\$path" + "\$subfolder" + "\$subfolder2")
                                remove-psdrive -name x
                                }     
                        }
                        
                }
            }   
        } 
    }
}

Mark, check out the “Dealing with Depth” section of this article I posted on another topic. It should help with your endeavor.

Thank you

I just had to write something myself today dealing with this type of issue.

the easiest way I’ve found is through alphafs (unfortunately documentation for calling these .net classes in powershell isn’t really there, I managed to get alphafs to perform in the same way as get-childitem -recurse and avoid the length limit

first import the DLL as a module, and then it is a simple as: something like this:
$folders = [alphaleonis.win32.filesystem.directory]::EnumerateDirectories($inputpath,‘*’,[System.IO.SearchOption]::AllDirectories)

documentation on the enumerate directories call is here:

you can download the alphafs code from the homepage here:AlphaFS – AlphaFS – AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes.

Long File Names that exceed the limit- the name of the tool is Long Path Tool. It can solve the problem.

Oxana

You can check this script from Boe Prox: https://gallery.technet.microsoft.com/scriptcenter/Get-Deeply-Nested-Files-a2148fd7

And his blogpost about it: