Advertisement
Ben_S

Summarize the Last 15 File Dates and Counts

Jan 28th, 2024 (edited)
1,541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.36 KB | Source Code | 0 0
  1. #---  FolderDayQty.ps1 ---
  2. #  Summarize the Last 15 File Dates and Counts
  3. #
  4. #  This PowerShell searches the files in the passed folder and
  5. #  groups the top 15 file dates sorted by date (decending),
  6. #  the number of days ago it was from today, and
  7. #  a count of the number of files on each date.
  8. #  
  9. #  Example output:
  10. #      Days    Media Date      Files
  11. #      2       10/05/2024 Sat  97
  12. #      3       10/04/2024 Fri  23
  13. #      12      09/25/2024 Wed  20
  14. #      13      09/24/2024 Tue  5
  15. #      15      09/22/2024 Sun  40
  16. #      16      09/21/2024 Sat  5
  17. #      17      09/20/2024 Fri  39
  18. #      18      09/19/2024 Thu  10
  19. #      20      09/17/2024 Tue  1
  20. #      22      09/15/2024 Sun  10
  21. #      25      09/12/2024 Thu  6
  22. #      27      09/10/2024 Tue  3
  23. #      29      09/08/2024 Sun  4
  24. #      31      09/06/2024 Fri  1
  25. #      32      09/05/2024 Thu  5
  26. #      
  27. #  This output is followed by a prompt in the calling batch file
  28. #  asking the user how may days ago to include in the archive
  29. #  process.  The Maxto M3 helmet camera is like a DVR so it
  30. #  continuously records and deletes old files as space is needed.  
  31. #  The batch file copies recordings from the camera card to a hard
  32. #  drive.  This output helps the user decide how far back the
  33. #  archive should go.  If the user picks more days than necessary,
  34. #  those days are still attempted to xcopy if they don't already
  35. #  exist.  (It doesn't hurt anything but it wastes time.)  The
  36. #  calling batch file will create an archive folder for each media
  37. #  date so files are grouped by recording date.
  38. #
  39. #  Shared online:   https://pastebin.com/KwZQk3uh
  40. #                   https://www.facebook.com/groups/maxtom3/posts/7992261460846946/
  41. #  This is used by Archive_M3_MOVIE_EF.bat, https://pastebin.com/LT5XESs0
  42. #  File generated with Phind.com AI
  43. #  Configured by Ben Sacherich - 11/19/2023 - https://www.facebook.com/groups/maxtom3
  44. #  BS 1/28/2024     Updated to sort dates as dates and not as strings.
  45. #  BS 10/7/2024     Added day of week "ddd" to the output and updated comments.
  46. #
  47.  
  48. param(
  49.     [string]$SourceFolder = ""
  50. )
  51. # $SourceFolder = "F:\CARDV\PHOTO"
  52.  
  53. if (-not (Test-Path $SourceFolder -PathType Container)) {
  54.     Write-Host "Source folder not found: $SourceFolder"
  55.     exit 1
  56. }
  57.  
  58. # Get all files in the source folder
  59. $files = Get-ChildItem -Path $SourceFolder
  60.  
  61. # Create a hashtable to store counts based on date
  62. $dateCounts = @{}
  63.  
  64. # Loop through each file
  65. foreach ($file in $files) {
  66.     # Get the date without the time
  67.     $date = $file.LastWriteTime.Date
  68.     # Write-Host $date
  69.  
  70.     # Check if the date is already in the hashtable
  71.     if ($dateCounts.ContainsKey($date)) {
  72.         # Increment the count for the date
  73.         $dateCounts[$date]++
  74.     } else {
  75.         # Add the date to the hashtable with a count of 1
  76.         $dateCounts[$date] = 1
  77.     }
  78. }
  79.  
  80. # Sort the hashtable by keys in descending order
  81. $sortedDates = $dateCounts.GetEnumerator() | Sort-Object -Property Name -Descending
  82.  
  83. # Select the top 15 dates
  84. $top5Dates = $sortedDates | Select-Object -First 15
  85.  
  86. # Display the counts for these dates and the number of days back they are from today
  87. Write-Host "Days`tMedia Date`tFiles"
  88. foreach ($date in $top5Dates) {
  89.     $count = $dateCounts[$date.Name]
  90.     $daysBack = (Get-Date).Subtract($date.Name).Days
  91.     Write-Host "$daysBack`t$($date.Name.ToString('MM/dd/yyyy ddd'))`t$count"
  92. }
  93.  
  94. exit
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement