Hi, I’m hoping you can help. I need to be able to extract information from a very large folder structure containing over 1 million files. The information I need includes:
-
Newest file Name & Date
-
Count of newest files modified within 3 moths of newest file
-
Total File Count & Size
All of the above can be easily extracted once a collection is made, but I don’t know the most efficient way to collect it. I have used two different approaches but each has its flaws and I wondered if there is a better approach.
The following approach is far quicker than the next approach but it consumes a huge amount of memory. Also, if I run it in a ForEach loop to process multiple large folders, the memory never gets returned after each instance and crashes the computer before the script completes (at which point memory would be returned):
remove-variable FolderFiles -ErrorAction SilentlyContinue
$FolderFiles=Get-ChildItem -LiteralPath "C:\TEMP" -Recurse -File -force | Select-Object Name, FullName, LastWriteTime, Length | Sort-Object LastWriteTime -Descending
$FolderFiles
The following approach does not consume large amounts of memory but it takes many times longer to complete each search:
remove-variable FolderFiles -ErrorAction SilentlyContinue
$FolderFiles=@()
Get-ChildItem -LiteralPath "C:\TEMP" -Recurse -File -force | Select-Object Name, FullName, LastWriteTime, Length | Sort-Object LastWriteTime -Descending | foreach-Object {
remove-variable filedata -ErrorAction SilentlyContinue
$fileData = [PSCustomObject]@{
"Name"=$_.Name
"FullName"=$_.FullName
"LastWriteTime"=$_.LastWriteTime
"Length"=$_.Length
}
$FolderFiles+=$filedata
}
$FolderFiles | Format-Table
I’ve used C:\TEMP in the above examples.
Is there a more efficient way of capturing large volumes of file information into an array that can then be interrogated?. I know that += isn’t the best approach but I’m not familiar with the best way to achieve what I’m after.
Thanks.
