Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Incorrect Total Folder Size Calculation - Please assist

$
0
0

Guys,

I use the below script to calculate a folder size and size of immidiate child folders.My output will be like below.

Folder Sizes for "C:\temp"

Run on 09/09/2014 12:02:01 PM

Folder NameOwnerCreated onLast UpdatedSize
C:\temp**************8/11/2014 3:54:12 PM9/9/2014 12:01:19 PM303.66 MB
C:\temp\professional**************9/8/2014 11:43:14 AM9/8/2014 1:15:35 PM303.64 MB
C:\temp\Sample*************9/8/2014 12:38:55 PM9/8/2014 12:39:03 PM0.01 MB

Total Space Used In "C:\temp": 607.31 MB

Error in my script is calculating the total space used ---> It is calulating the C:\temp whole folder size along with internal folder size also,

The corrent output has to be "Total Space Used In "C:\temp": 303.66 MB"

Where is the logic missed in the script, please help.

Script below:

Param (
 [string]$Path = "C:\temp",
 [string]$ReportPath = "C:\temp\Sample"
)

Function AddObject {
 Param (
  $FileObject
 )
 $Size = [double]($FSO.GetFolder($FileObject.FullName).Size)
 $Script:TotSize += $Size
 If ($Size)
 { $NiceSize = CalculateSize $Size
 }
 Else
 { $NiceSize = "0.00 MB"
        $Size = 0
 }
 $Script:Report += New-Object PSObject -Property @{
  'Folder Name' = $FileObject.FullName
  'Created on' = $FileObject.CreationTime
  'Last Updated' = $FileObject.LastWriteTime
  Size = $NiceSize
        RawSize = $Size
  Owner = (Get-Acl $FileObject.FullName).Owner
 }
}

Function CalculateSize {
 Param (
  [double]$Size
 )
 If ($Size -gt 1000000000)
 { $ReturnSize = "{0:N2} GB" -f ($Size / 1GB)
 }
 Else
 { $ReturnSize = "{0:N2} MB" -f ($Size / 1MB)
 }
 Return $ReturnSize
}

cls
$Report = @()
$TotSize = 0
$FSO = New-Object -ComObject Scripting.FileSystemObject

#First get the properties of the starting path
$Root = Get-Item -Path $Path
AddObject $Root

#Now loop through all the subfolders
ForEach ($Folder in (Get-ChildItem -Path $Path | Where { $_.PSisContainer }))
{ AddObject $Folder
}

#Create the HTML for our report
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
<Title>
Folder Sizes for "$Path"
</Title>
"@

$TotSize = CalculateSize $TotSize

$Pre = "<h1>Folder Sizes for ""$Path""</h1><h2>Run on $(Get-Date -f 'MM/dd/yyyy hh:mm:ss tt')</h2>"
$Post = "<h2>Total Space Used In ""$($Path)"":  $TotSize</h2>"

#Create the report and save it to a file
$Report | Sort RawSize -Descending | Select 'Folder Name',Owner,'Created On','Last Updated',Size | ConvertTo-Html -

PreContent $Pre -PostContent $Post -Head $Header | Out-File $ReportPath\FolderSizes.html

#Display the report in your default browser
& $ReportPath\FolderSizes.html


Viewing all articles
Browse latest Browse all 6937

Trending Articles