0

我希望查询我的文档文件夹的大小,但还要列出子文件夹的大小并导出到目录。我在网上找到了一些,并试图调整我的需求,但导出部分有问题,非常感谢任何帮助!:

$startFolder = "C:\Users"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

add-content \\server\logs$\DirSize\log.log "$(gc env:computername)"

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems){
    {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB" | add-content \\server\logs$\DirSize\log.log}}

现在它正在打开 C:\User\"User" 下的所有文件夹,我可以限制为 C:\Users\"User" 下的一个子目录吗?再次感谢!

4

2 回答 2

3

给这个:

$log = "C:\logfile.log" 
$startFolder = "C:\Users"
$colItems = Get-ChildItem $startFolder  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems){
   $itemSum = Get-ChildItem ("$startFolder\" + $i.Name) -recurse | Measure-Object -property length -sum
   "$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1MB) + " MB" >> $log
   }

这将输出给定 $startFolder 中的所有文件夹大小,并将结果输出到一个日志文件 ($log),如下所示:

启动文件:'C:\test'

C:\test\config -- 0.00 MB
C:\test\docs -- 0.38 MB
C:\test\files -- 0.03 MB
C:\test\Help -- 7.28 MB
C:\test\inbox -- 0.00 MB
C:\test\install -- 0.00 MB
C:\test\jobs -- 0.00 MB

降级2级:

$log = "C:\logfile.log" 
$startFolder = "C:\Users"
$colItems = Get-ChildItem $startFolder  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems){
   $itemSum = Get-ChildItem ("$startFolder\" + $i.Name) -recurse | Measure-Object -property length -sum
   "$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1MB) + " MB" >> $log
   $colItems2 = Get-ChildItem ("$startFolder\" + $i.Name)  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
   foreach ($j in $colItems2){
        If($j -ne $null){
            $itemSum2 = Get-ChildItem ("$startFolder\" + $i.Name +"\"+ $j.Name) -recurse | Measure-Object -property length -sum
            "-->$startFolder\$i\$j -- " + "{0:N2}" -f ($itemSum2.sum / 1MB) + " MB" >> $log
        }
   }
}
于 2013-04-29T15:02:27.923 回答
1

这将递归树的所有子目录并获得它们各自的大小:

Param($Path = $(throw "Missing directory")
,$CSVFile = $(throw "Missing logfile")
)

Function GerDirSize($PathDir)
{
    #Gets subdirectories
    $dirs = Get-ChildItem $PathDir | Where-Object {$_.PsIsContainer}

    #Check if the list is empty
    if($dirs -ne $null)
    {
        foreach($dir in $dirs)
        {
            #Gets size of current directory
            $itemSum = Get-ChildItem $dir.FullName -recurse | Where-Object {!$_.PsIsContainer} | Measure-Object -Property length -Sum
            $itemSum = "{0:N2}" -F ($itemSum.sum / 1MB)
            $result = $dir.FullName + ";" + $itemSum
            Add-Content $CSVFile -Value $result

            #Gets size of subdirectories
            GerDirSize($dir.FullName)
        }
    }
}

GerDirSize $Path

将其命名为 GetDirSize.ps1 并按如下方式使用它:

GetDirSize.ps1 "c:\temp" "c:\temp\logfile.log"

结果将是:

C:\temp\directory1;10.34
C:\temp\directory1\subdirectory1;2.07
C:\temp\directory1\subdirectory1\subsub1;0.00
C:\temp\directory1\subdirectory2;6.21
C:\temp\directory1\subdirectory3;0.00
C:\temp\directory2;4.14
C:\temp\directory3;10.34
C:\temp\directory3\subdirectory1;4.14
C:\temp\directory3\subdirectory1\subdirectory1;2.07
C:\temp\directory4;0.00
于 2014-10-09T17:31:34.753 回答