Forum Discussion
Best Practice - how to get file age and size distribution in a folder
Hello Community
I am seeking a little help to improve my PS script.
It works, but I feel it contains some overhead code and I might head into the wrong direction.
For the moment I am not worried about the bug in line 32.
Do I depend on a hashtable for creating a statistics about the age distribution of files?
I am also interested in summing up the file size per day.
Before I spend more time with that script, I am interested in your opinion
PS G:\formate\PowerShell> .\ListModifiedDateInFolder.ps1 -Path j:
j:
579
Name Value Percentage
---- ----- ----------
22.09.2022 36 16.0833333333333
23.09.2022 57 10.1578947368421
24.09.2022 94 6.15957446808511
25.09.2022 120 4.825
26.09.2022 72 8.04166666666667
27.09.2022 121 4.78512396694215
28.09.2022 79 7.32911392405063
param([string]$Path = (pwd).Path)
$fileStats = @{}
Get-ChildItem -Path $Path |
Select-Object Name,FullName,CreationTime, @{Name="LastModifiedTime"; Expression={$_.LastWriteTime.ToString("dd.MM.yyyy")}} |
ForEach-Object -Process {
$key = $_.LastModifiedTime
if($fileStats.ContainsKey($key)) {
$fileStats[$key] = $fileStats[$key] + 1
} else {
$fileStats.Add($key,1)
}
}
$Path
(Get-ChildItem -Path $Path).Length
$fileStats.GetEnumerator() | Sort Name | Select-Object Name, Value, @{Name="Percentage"; Expression={ (Get-ChildItem -Path
- JoP_SGCopper Contributor
Hello matjung,
I hope I understood this correct. You want to have the amount of files and the summarized size in a folder per day. I would suggest to use group-object and measure-object like this:Get-ChildItem -file -Path . | Select-Object Name,FullName, Length, @{Name="LastModifiedTime"; Expression={$_.LastWriteTime.ToString("dd.MM.yyyy")}} | group-object -Property LastModifiedTime | Select-Object Name,Count,@{n='Sum Size(MB)';e={ ($_.Group|Measure-Object -Property Length -Sum).Sum/1MB }}
- matjungCopper ContributorThanks @Joachim group-object helped a lot.
- matjungCopper Contributor
Here is my current Output + Solution
The approach with Expression feels weird.
I will have a closer look into Measure with Script Blocks.
PS G:\formate\PowerShell> . 'g:\formate\PowerShell\ListModifiedDateInFolder.ps1' j:
j: Files 579.00 Size 3’648.17 MBName Count Age% Filesize (MB) Volume (%)
---- ----- ---- ------------- ----------
22.09.2022 36 6.22% 222.05 6.09%
23.09.2022 57 9.84% 355.84 9.75%
24.09.2022 94 16.23% 592.00 16.23%
25.09.2022 120 20.73% 754.52 20.68%
26.09.2022 72 12.44% 460.43 12.62%
27.09.2022 121 20.90% 766.22 21.00%
28.09.2022 79 13.64% 497.10 13.63%param([string]$Path = (Get-Location).Path) $totalStats = Get-ChildItem -File -Path $Path| Measure-Object -Property length -Sum "{0} Files {1:N} Size {2:N} MB" -f $Path, $totalStats.Count, ($totalStats.Sum / 1MB) Get-ChildItem -file -Path $Path | Select-Object Name,FullName, Length, @{Name="LastModifiedTime"; Expression={$_.LastWriteTime.ToString("dd.MM.yyyy")}} | group-object -Property LastModifiedTime | Select-Object Name, Count, @{Name="Age%"; Expression= { ($_.Count / $totalStats.Count).ToString("P")}}, @{Name="Filesize (MB)"; Expression = { (($_.Group | Measure-Object -Property Length -Sum).Sum) / 1MB}}, @{Name="Volume (%)"; Expression = { ((($_.Group | Measure-Object -Property Length -Sum).Sum) / $totalStats.Sum).ToString("P") }} | Sort Name | Format-Table