2

My script giving error "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 248 characters." and "Measure-Object : The property "length" cannot be found in the input for any objects". It supposed to create a text file with list of folders arranged by size. However,I get errors above and some folders get size 0.0 Mb. Do you have any suggestions?

    $invocation = (Get-Variable MyInvocation).Value
    $directorypath = Split-Path $invocation.MyCommand.Path


Function Get-FolderName
{   
   Param (   
   $PathName
   )


$Array=@('\')

ForEach ($Folder in $PathName)
 {

$a=[string]$Folder

   $a=$a.split("\")[-1]

   $a=$a-replace '["}"]',''
   $Array+=$a
     }



 ForEach ($Folder in $Array)
 {

  $colItems = (Get-ChildItem $directorypath\$Folder -recurse -ErrorAction   Inquire | Measure-Object -property length -sum -ErrorAction Inquire )
 $colItems ="{0:N2}" -f ($colItems.sum / 1MB)
 $colItems =[String]$colItems-replace '[","]',''
 #$colItems =[String]$colItems-replace '["."]',''
 $colItems = [float]$colItems
 $colItemsGB = [float]($colItems /1024)| % { '{0:0.##}' -f $_ }



 #$colItems=(Get-ChildItem $directorypath\$Folder -Recurse | Measure-Object Length -Sum)

 [PSCustomObject]@{
            Folder = $Folder;
            Size_MB=$colItems 
            Size_GB=$colItemsGB
                                      }
                          }

                          }


Function Check-FileName{
Param (

   $PathName
   )

$b=[string]$directorypath.split("\")[-1]

if(Test-Path $directorypath\$b.txt)
{
 Clear-Content $directorypath\$b.txt
 $content = Get-FolderName $PathName 
 return $content|Sort-Object -property Size_MB -Descending | out-file   $b".txt"

 }
 else {
    $content = Get-FolderName $PathName 
    return $content |Sort-Object -property Size_MB -Descending| out-file     $b".txt"

  }

  }


   $aa = dir -Directory | Select FullName

   Check-FileName $aa
user6144397
  • 21
  • 1
  • 4
  • Possible duplicate of [Powershell - Delete directory regardless of 260 char limit](http://stackoverflow.com/questions/16392765/powershell-delete-directory-regardless-of-260-char-limit) – JamesQMurphy Apr 01 '16 at 10:19

1 Answers1

1

There is almost nothing you can do about it. Situation is application specific. Windows API for accessing longer file paths exists but its not certain if application developers used it. For instance, TFS server 2015 can't use longer file paths also and there is no fix that I am aware of. This is typically problem with Java apps since they can have very long class paths.

You could use AlphaFS.

Other alternative includes using New-PSDrive which involves some work to join the results later.

majkinetor
  • 8,730
  • 9
  • 54
  • 72