Advertisement
Ben_S

List Top 15 Folders by Date

Jan 28th, 2024 (edited)
1,341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.80 KB | Source Code | 0 0
  1. #---  FolderDayQty.ps1 ---
  2. #  List the top 15 folders sorted by date (decending) and
  3. #  a count of the number of files in each folder.
  4. #
  5. #  Shared online:   https://pastebin.com/KwZQk3uh
  6. #                   https://www.facebook.com/groups/maxtom3/posts/7992261460846946/
  7. #  This is used by Archive_M3_MOVIE_EF.bat, https://pastebin.com/LT5XESs0
  8. #  File  generated with Phind.com AI
  9. #  Configured by Ben Sacherich - 11/19/2023
  10. #  Updated 1/28/2024 to sort dates as dates and not as strings.
  11.  
  12. param(
  13.     [string]$SourceFolder = ""
  14. )
  15. # $SourceFolder = "F:\CARDV\PHOTO"
  16.  
  17. if (-not (Test-Path $SourceFolder -PathType Container)) {
  18.     Write-Host "Source folder not found: $SourceFolder"
  19.     exit 1
  20. }
  21.  
  22. # Get all files in the source folder
  23. $files = Get-ChildItem -Path $SourceFolder
  24.  
  25. # Create a hashtable to store counts based on date
  26. $dateCounts = @{}
  27.  
  28. # Loop through each file
  29. foreach ($file in $files) {
  30.     # Get the date without the time
  31.     $date = $file.LastWriteTime.Date
  32.     # Write-Host $date
  33.  
  34.     # Check if the date is already in the hashtable
  35.     if ($dateCounts.ContainsKey($date)) {
  36.         # Increment the count for the date
  37.         $dateCounts[$date]++
  38.     } else {
  39.         # Add the date to the hashtable with a count of 1
  40.         $dateCounts[$date] = 1
  41.     }
  42. }
  43.  
  44. # Sort the hashtable by keys in descending order
  45. $sortedDates = $dateCounts.GetEnumerator() | Sort-Object -Property Name -Descending
  46.  
  47. # Select the top 15 dates
  48. $top5Dates = $sortedDates | Select-Object -First 15
  49.  
  50. # Display the counts for these dates and the number of days back they are from today
  51. Write-Host "Days`tMedia Date`tFiles"
  52. foreach ($date in $top5Dates) {
  53.     $count = $dateCounts[$date.Name]
  54.     $daysBack = (Get-Date).Subtract($date.Name).Days
  55.     Write-Host "$daysBack`t$($date.Name.ToString('MM/dd/yyyy'))`t$count"
  56. }
  57.  
  58. exit
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement