获取函数中的文件夹大小

时间:2017-06-28 17:40:25

标签: powershell powershell-v3.0

我正在尝试获取每个文件夹的大小以及其子文件夹以及所有者,路径和上次修改日期 - 也达到5的深度。我有除了完成文件夹大小之外的所有内容我试图以MB为单位获取大小

这是我的代码:

Function Get-Depth {
    Param(
        [String]$Path = '/Users/demo/main',
        [String]$Filter = "*",
        [Int]$ToDepth = 4,
        [Int]$CurrentDepth = 0
    )
    #incrimintation
    $CurrentDepth++

 #obtains the path and passes the filter values. KEEP in mind that level 1 is 0.
    Get-ChildItem $Path | %{
        $_ | ?{ $_.Name -Like $Filter }
 #if thier is a folder, use the depth and run function until to depth value is 4
         If ($_.PsIsContainer) {
         If ($CurrentDepth -le $ToDepth) {

         # Call to function
         #adds the filter values and depth to the path..
         Get-Depth -Path $_.FullName -Filter $Filter `
          -ToDepth $ToDepth -CurrentDepth $CurrentDepth
        }
     }
   }

}


#just calling the function and and adding what we want!

Get-Depth|? {$_.PsIsContainer}| select @{Name='Date Modified'; 
Expression={$_.LastWriteTime.ToString('MM/dd/yyyy')}},
@{Name='Owner'; E={(($_.GetAccessControl().Owner.Split('\'))[1])}}, 
Fullname 

我想要获得的结构

 h:\demo\1st level
 h:\demo\1st level\2nd level
 h:\demo\1st level\2nd level\3rd level
 h:\demo\1st level\2nd level\3rd level\4th level\
 h:\demo\1st level\2nd level\3rd level\4th level\5th level

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

(Get-ChildItem "c:\temp" -directory -Recurse  | %{

$charCount = ($_.Fullname.ToCharArray() | Where {$_ -eq '\'} | Measure-Object).Count

if ($charCount -gt 5) { continue}

$size=(Get-ChildItem $_.Fullname -file -Recurse | Measure-Object -property length -sum).Sum

[pscustomobject]@{
'Date Modified'=$_.LastWriteTime.ToString('MM/dd/yyyy')

Owner=$_.GetAccessControl().Owner.Split('\')[1]
Fullname=$_.Fullname
Size= if ($size) {[Math]::Round($size / 1MB,2,[MidPointRounding]::AwayFromZero)       } else {0}
LevelDir=$charCount
}

}) | sort Fullname, LevelDir