Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.
Here is a function to get document statistics for a specified folder. Thanks to Kevin Van Haaren for providing this via the Bentley Communities page.
FUNCTION Get-QXFolderStats {
<#
.SYNOPSIS
Return statistics about documents in a folder and sub-folders
.DESCRIPTION
Return statistics about documents in a folder and sub-folders.
Returns:
TopFolder => Folder object of top folder
FolderCount => Count of folders (including top folder)
FileCount => Count of files
TotalSize => Size of all files in bytes, KB, MB, GB
LargestFile => Size of largest file in bytes, KB, MB, GB
OldestFile => Create Date/time of oldest file
LatestFile => Update Date/time of latest file
.PARAMETER Folder
ProjectWise folder object
.EXAMPLE
$pwFolder = Show-PWFolderBrowserDialog
$folderStats = Get-QXFolderStats -Folder $pwFolder
.OUTPUTS
Hashtable with following properties:
TopFolder => Folder object of top folder
FolderCount => Count of folders (including top folder)
FileCount => Count of files
TotalSize => Size of all files in bytes
LargestFile => Size of largest file in bytes
OldestFile => Create Date/time of oldest file
LatestFile => Update Date/time of latest file
#>
#region Parameters
[CmdletBinding(DefaultParameterSetName='Default')]
Param (
[Parameter(
Position = 0,
Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,
HelpMessage='ProjectWise folder'
)]
[PWPS_DAB.CommonTypes+ProjectWiseFolder]$Folder
)
#endregion Parameters
BEGIN {
$CmdletName = $MyInvocation.MyCommand.Name
$StartTime = Get-Date
Write-Host "[BEGIN] $Start - Entering '$CmdletName' Function..." -ForegroundColor Cyan
} #end BEGIN...
PROCESS {
try {
$stats = @{
TopFolder = $null # returns actual folder object (with all properties)
FolderCount = $null
FileCount = $null
TotalSize = $null # in bytes
LargestFile = $null # in bytes
OldestFile = $null # date/time
LatestFile = $null # date/time
}
[void]$Folder.GetFolderProperties()
$stats.TopFolder = $Folder.FullPath
$folderList = Get-PWFolders -FolderID $Folder.ProjectID
$stats.FolderCount = $folderList.Count
$docSplat = @{
FolderPath = $Folder.FullPath
GetVersionsToo = $true
JustThisFolder = $false
}
$docList = Get-PWDocumentsBySearchExtended @docSplat -WarningAction SilentlyContinue
if (-not [string]::IsNullOrEmpty($docList)) {
$stats.FileCount = $docList.count
$sizeStats = $docList | Measure-Object -Property FileSize -Sum -Maximum
$TotalSize = 0
$TotalSizeUnits = [string]::Empty
switch ($sizeStats.Sum) {
{$_ -ge 1KB -and $_ -lt 1MB} {
$TotalSize = [Math]::Round($sizeStats.Sum / 1KB, 2)
$TotalSizeUnits = 'KB'
break
}
{$_ -ge 1MB -and $_ -lt 1GB} {
$TotalSize = [Math]::Round($sizeStats.Sum / 1MB, 2)
$TotalSizeUnits = 'MB'
break
}
{$_ -ge 1GB} {
$TotalSize = [Math]::Round($sizeStats.Sum / 1GB, 2)
$TotalSizeUnits = 'GB'
break
}
Default {
break
}
}
$Maximum = 0
$MaximumUnits = [string]::Empty
switch ($sizeStats.Maximum) {
{$_ -ge 1KB -and $_ -lt 1MB} {
$Maximum = [Math]::Round($sizeStats.Maximum / 1KB, 2)
$MaximumUnits = 'KB'
break
}
{$_ -ge 1MB -and $_ -lt 1GB} {
$Maximum = [Math]::Round($sizeStats.Maximum / 1MB, 2)
$MaximumUnits = 'MB'
break
}
{$_ -ge 1GB} {
$Maximum = [Math]::Round($sizeStats.Maximum / 1GB, 2)
$MaximumUnits = 'GB'
break
}
Default {
break
}
}
$stats.TotalSize = "$TotalSize $TotalSizeUnits" # $sizeStats.Sum
$stats.LargestFile = "$Maximum $MaximumUnits" # $sizeStats.Maximum
$stats.OldestFile = ($docList | Measure-Object -Property CreateDate -Minimum).Minimum
$stats.LatestFile = ($docList | Measure-Object -Property FileUpdateDate -Maximum).Maximum
} else {
Write-Warning "No documents found in folder $($folder.FullPath)"
}
return $stats
} catch {
Write-Warning -Message $_
}
} #end PROCESS...
END{
$EndTime = Get-Date
Write-Host "[END] It took $($EndTime - $StartTime) to complete the process." -ForegroundColor Cyan
Write-Host "[END] $EndTime - Exiting '$CmdletName' Function..." -ForegroundColor Cyan
} #end END...
} # End FUNCTION Get-QXFolderStats...
Experiment with it and have fun.
Hopefully, you find this useful. Please let me know if you have any questions or comments. If you like this post, please click the Like button at the bottom of the page. And thank you for checking out my blog.
