【问题标题】:get folder size in a function在函数中获取文件夹大小
【发布时间】:2017-06-29 02:28:40
【问题描述】:

我正在尝试获取每个文件夹及其其子文件夹的大小以及所有者、路径和上次修改日期 - 深度也最多为 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

谢谢!

【问题讨论】:

  • 我会避免重新发明轮子,只使用du.exe
  • @Bill_Stewart 谢谢,但是有什么办法可以解决这个问题吗?我正在尝试获取非常具体的参数。
  • 对不起,我不明白你的问题。

标签: powershell powershell-3.0


【解决方案1】:

试试这个:

(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

【讨论】:

  • 谢谢,但这并没有获得以 MB 为单位的大小,也不符合 5 的深度。我也在尝试获取所有子文件夹的大小
  • 试试我的修改 ;)
  • 由于某种原因它没有出现在文件树格式中,请参阅更新的问题。
  • 我仍然无法实现问题中指定的文件路径。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2014-04-01
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多