Thursday, January 5, 2012

Powershell (v2) - Finding Oldest/Newest Files in a Folder

As a part of my continuing effort to get a decent analysis of my 474,000+ file-loaded folder I used Powershell to get the CreationTime properties of the oldest and newest files for reach document type.  Unfortunately, this solution is pretty old school, but, I was in a hurry, so, I didn't have time to whip anything fancy.  I had already figured out I only had two file types.  I could have automated more by using a Select-Object -Unique approach to build a list of extensions, but, didn't have time to play with it any further.
$path = "C:\users\will\documents\files";
dir $path | Where-Object {$_.extension -eq ".pdf"} | sort-Object -prop LastWriteTime | select -last 1 name,creationtime
dir $path | Where-Object {$_.extension -eq ".pdf"} | sort-Object -prop LastWriteTime | select -first 1 name,creationtime
dir $path | Where-Object {$_.extension -eq ".html"} | sort-Object -prop LastWriteTime | select -last 1 name,creationtime
dir $path | Where-Object {$_.extension -eq ".html"} | sort-Object -prop LastWriteTime | select -first 1 name,creationtime
In each of these cases I was able to simply get Name and CreationTime properties  after sorting on LastWriteTime.

0 comments:

Post a Comment