Monthly archives: July 2012

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"

Blog

Search Posts

Recent Comments

  1. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: FYI, after posting the issue on technet + windows-noob.com with no replies I decided to do a fresh Windows...

  2. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: Hi and thanks for your post. I originally installed SQL 2016 with SP1 on a Windows server 2016 and got...

  3. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Timothy: Update Rollup 2.

  4. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Catwiesl: You write "Microsoft System Center Data Protection Manager (DPM) officially supports SQL 2016 with U...

  5. Re: PowerShell: Quickly Finding the Oldest and Newest Files in a Folder
    Neki: i would like to compare a local file with a remote file and download the remote file if its newer than...

  6. Re: RD Tabs: Tabbed Windows Remote Desktops
    lukas: Fantastic software though I am having issues saving passwords. As soon as I kill the program the setting...

  7. Re: Easy Way to Change Permissions on the Windows Server Scheduled Tasks Folder (C:\Windows\Tasks)
    George: From another server, using an admin account, I opened \\SERVER\c$\windows in Windows Explorer. Tasks...

  8. Re: PowerShell: Quickly Finding the Oldest and Newest Files in a Folder
    poo: would be great if you can help... i would like to compare a local file with a remote file and download...

  9. Re: Mystery of the Server 2008 + IIS7 + OLE = "MDAC Not Installed" Error
    Gwasshoppa: Interesting... I had this exact same error... One day the server was performing the task relating to...

  10. Re: Dish Network's DVR External Hard Drive Feature ROCKS!
    Linda: I am wanting to transfer also but I don't know which external hard drive I need, Help please

Archive

Tag Cloud