Author Posts

September 29, 2015 at 8:52 am

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

September 29, 2015 at 10:03 am

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

https://www.linkedin.com/pulse/20140913012734-17649382-fma-archive-recall

October 2, 2015 at 11:13 am

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:
http://alphafs.alphaleonis.com/doc/2.0/api/html/D91BDFAC.htm

you can download the alphafs code from the homepage here:http://alphafs.alphaleonis.com/index.html

March 4, 2016 at 8:52 pm

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

Oxana