The purpose of the script is the following:
- Print the number of files recursively found within a directory (omitting folders themselves)
- Print the total sum file size of the directory
- Not crash the computer because of massive memory use.
So far (3) is the tough part.
Here is what I have written and tested so far. This works perfectly well on folders with a hundred or even a thousand files:
$hostname=hostname
$directory = "foo"
$dteCurrentDate = Get-Date –f "yyyy/MM/dd"
$FolderItems = Get-ChildItem $directory -recurse
$Measurement = $FolderItems | Measure-Object -property length -sum
$colitems = $FolderItems | measure-Object -property length -sum
"$hostname;{0:N2}" -f ($colitems.sum / 1MB) + "MB;" + $Measurement.count + " files;" + "$dteCurrentDate"
On folders with millions of files, however, the $colitems variable becomes so massive from the collection of information of millions of files that it makes the system unstable. Is there a more efficient way to draw and store this information?