Posted Saturday, January 14, 2012 in Old JamesCMS Posts
Need a rediculously complicated method of determining the size of root folders in a directory? Well here's the script for you. The primary use of this is when you need to get the size of very many very large folders such as user profile folders on a share drive.
{{Powershell}}
function Git-RootFoldersSizes2 ($path, $threads) {
@”
`$path = `$args[0]
`$sum = Get-ChildItem `$path -recurse -ea 0 -Force |
measure -Property Length -Sum |
Select -ExpandProperty Sum
“{0:N2}” -f (`$sum / 1MB)
`$nothing
“@ | out-file “$env:temp\computesize.ps1”
$sw = new-object System.Diagnostics.StopWatch
$sw.start()
$report = new-object system.collections.arraylist
Remove-Job -state completed
if ($threads -eq $null) {$threads = 4} else {$threads --}
Get-ChildItem $path | where {$_.mode -eq “d----”} |%{
$temp = “” | select Name, Path, CreationTime, LastWriteTime, Size
write-progress -activity “Discovering Folders” -status $_.Name
$temp.Name = $_.Name
$temp.Path = $_.FullName
$temp.CreationTime = $_.CreationTime
$temp.LastWriteTime = $_.LastWriteTime
$report.add($temp) | out-null
}
$total = $report.count
for ($i = 0; $i -lt $total; $i ++)
{
$running = @(get-job -state running).count
while ($running -gt $threads)
{
sleep .1
$running = @(get-job -state running).count
}
start-job -FilePath “$env:temp\computesize.ps1” -ArgumentList $report[$i].Path -Name $report[$i].Name | out-null
if ((get-job -state Running | select Name) -ne $null)
{
$complete = @(get-job -state completed).count
$percent = (($complete / $total) * 100)
$status = “Threads Running: “ + @(get-job -state running).count + “ Threads Completed: “ + $complete + “ Remaining: “ + ($total - $complete)
write-progress -activity “Computing Folder Sizes” -status $status -PercentComplete $percent
}
}
$running = @(get-job -state running).count
while ($running -gt 0)
{
wait-job -state Running -any | out-null
$running = @(get-job -state running).count
if ((get-job -state Running | select Name) -ne $null)
{
$complete = @(get-job -state completed).count
$percent = (($complete / $total) * 100)
$status = “Threads Running: “ + @(get-job -state running).count + “ Threads Completed: “ + $complete + “ Remaining: “ + ($total - $complete)
write-progress -activity “Computing Folder Sizes” -status $status -PercentComplete $percent
}
}
for ($i = 0; $i -lt $total; $i ++)
{
$report[$i].Size = Receive-Job -Name $report[$i].Name
}
$report | Select name, Size, CreationTime, LastWriteTime | sort {[double] $_.Size} -descending
$sw.stop()
$elapsed = “{0:00} min {1:00}.{2:0000} sec” -f $sw.Elapsed.Minutes, $sw.Elapsed.Seconds, $sw.Elapsed.Milliseconds
write-host “”
write-host $elapsed -foregroundcolor yellow
}