Query Folder tree for Size and export to a log on

2019-09-15 19:35发布

问题:

Im looking to query the my documents folder for size, but also list the subfolders for size and export to a directory. I found some of this online and am trying to tweak to my needs but having issues with the export part, any help is GREATLY appreciated! :

$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}}

Right now it is opening all folders under the C:\User\"User", can I limit to just one subdirectory under C:\Users\"User"? Thanks again!

回答1:

Give this go:

$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
   }

This will output all the folders sizes within the given $startFolder and output the result to a log file ($log) that will look like this:

Start file: '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

Stepping down 2 levels:

$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
        }
   }
}


回答2:

This will recurse all subdirectories of the tree and get their respective sizes:

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

Name it GetDirSize.ps1 and use it as follows:

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

The result will be:

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