【发布时间】:2016-04-01 10:14:59
【问题描述】:
我的脚本给出错误“Get-ChildItem : The specified path, file name, or both are too long. 完全限定的文件名必须少于 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
【问题讨论】:
标签: powershell