我的脚本给出错误“Get-ChildItem:指定的路径,文件名或两者都太长 . 完全限定的文件名必须少于260个字符,目录名称必须少于248个字符 . ”并且“测量对象:属性”长度“在任何对象的输入中都找不到” . 它应该创建一个文本文件,其中包含按大小排列的文件夹列表 . 但是,我得到上面的错误,一些文件夹的大小为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
1 回答
你几乎无能为力 . 情况是特定于应用的 . 存在用于访问较长文件路径的Windows API,但如果应用程序开发人员使用它,则不确定 . 例如,TFS服务器2015也不能使用更长的文件路径,并且没有我所知道的修复 . 这通常是Java应用程序的问题,因为它们可以具有非常长的类路径 .
你可以使用AlphaFS .
其他替代方案包括使用
New-PSDrive
,这涉及到稍后加入结果的一些工作 .