Skip to content

$cs.File_GetFolderSize

Calculates the total size, in bytes, of every file under path, including all subfolders. Throws DirectoryNotFoundException if path doesn’t exist.

$cs.File_GetFolderSize(string path)

Full path of the folder to measure.

Long, the total size of the folder contents in bytes.

Terminal window
$logFolder = $global:AppLogFolder
if ($cs.File_ExistDir($logFolder))
{
$sizeInBytes = $cs.File_GetFolderSize($logFolder)
$cs.Job_WriteLog("Log folder size: $sizeInBytes bytes")
}

Sanity-check that an extraction step actually produced files, rather than silently leaving an empty folder:

Terminal window
$extractFolder = Join-Path $global:Packageroot "Extracted"
if ($cs.File_ExistDir($extractFolder))
{
$sizeInBytes = $cs.File_GetFolderSize($extractFolder)
if ($sizeInBytes -eq 0)
{
Exit-PSScript -exitcode 1 -exitmessage "Extraction produced an empty folder, aborting install"
}
}

Log how much space a data folder is using before removing it during cleanup:

Terminal window
$dataFolder = Join-Path $env:ProgramData "Contoso\Cache"
if ($cs.File_ExistDir($dataFolder))
{
$sizeInMb = [math]::Round($cs.File_GetFolderSize($dataFolder) / 1MB, 2)
$cs.Job_WriteLog("Removing cache folder ($sizeInMb MB) at $dataFolder")
$cs.File_DelTree($dataFolder)
}

$cs.File_ExistDir $cs.File_DelTree