From monthly archives: July 2012

We are pleased to present below all posts archived in 'July 2012'. If you still can't find what you are looking for, try using the search box.

Timothy

PowerShell: Quickly Finding the Oldest and Newest Files in a Folder

I whipped up this script to quickly find the oldest and newest files in a folder with PowerShell because we have some archive folders that have millions of files and it can crash Windows Explorer.  Other scripts I’ve seen online use PowerShell’s Where-Object after doing a sort on the entire collection, but that’s inefficient because it requires sorting millions of file records, which is slow.  What I’m doing is using ForEach-Object to track the oldest and newest dates as I parse through the directory list in whatever order it comes to me.  It saves a lot of time and memory.  Enjoy!

$olddate = [DateTime]::MaxValue $newdate = [DateTime]::MinValue $oldfn = "" $newfn = "" $path = "." get-childitem $path | ForEach-Object { if ($_.LastWriteTime -lt $olddate -and -not $_.PSIsContainer) { $oldfn = $_.Name $olddate = $_.LastWriteTime } if ($_.LastWriteTime -gt $newdate -and -not $_.PSIsContainer) { $newfn = $_.Name $newdate = $_.LastWriteTime } } $output = "" if ($oldfn -ne "") { $output += "`nOldest: " + $olddate + " -- " + $oldfn } if ($newfn -ne "") { $output += "`nNewest: " + $newdate + " -- " + $newfn } if ($output -eq "") { $output += "`nFolder is empty." } $output + "`n"

Recent Comments
  1. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    kAM aCOSTA: Thanks Edward !!!
  2. Re: 3.0 is coming...
    Dave: Very Cool!
  3. Re: In VB.Net, sending output to the console from a Windows "Forms" application
    clochardM33: Glorious
  4. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Andreas Hagberg: Edward, you are the man. It solved the problem right at the first try. Many thanks for the post.
  5. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Derek: Edward, great find. +1 on the fix... Thanks!!
  6. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    milo: Thanx MIKE - this helped me: DPM 2016 setup will fail if you have SQL Server Management Studio (SSMS...
  7. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Terry: Edward, you are the Man!!!! Looked for a solution for hours, then found your post and BAM!!! it worked...
  8. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Mr. JoeM: Edward! Thank you, saved me hours of work.
  9. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Brian: Thank you so much Edward! :-)
  10. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Tom: Thank you Edward! After beating my head against a wall for days, tried your suggestion out and lo and...