Powershell Get-ChildItem:指定的路径、文件名或两者都太长



我的脚本给出错误"Get-ChildItem:指定的路径、文件名或两者都太长。完全限定的文件名必须少于260个字符,目录名必须为少于248个字符。和"度量值对象:在任何对象的输入中都找不到属性"length"。它应该创建一个文本文件,其中包含按大小排列的文件夹列表。然而,我收到了上面的错误,一些文件夹的大小为0.0 Mb。你有什么建议吗?

    $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

对此几乎无能为力。具体情况取决于应用程序。用于访问较长文件路径的Windows API存在,但不确定应用程序开发人员是否使用了它。例如,TFS服务器2015也不能使用较长的文件路径,我不知道有什么修复方法。这通常是Java应用程序的问题,因为它们可能有很长的类路径。

您可以使用AlphaFS。

其他替代方案包括使用New-PSDrive,这涉及到稍后加入结果的一些工作。

相关内容

最新更新