PowerShell: quick custom object

TL;DR: I parse some filenames and pack the results into a quick custom object.

I found that I had a lot of files to go through, they were all dated very nicely:

Sadly, some of the dates in the filenames didn’t match the date modified, so I can’t use that to sort them.

And I can’t use the filename because of the date format doesn’t sort correctly. If only they had nicely formatted them as 2014-02-28, then all would be good.

PoSh it is then:

#get the files into some data 
$files = gci "\\folder"

# grab the names
# pick out the date with a regex, remember to out-null to get rid of the True, True, True, True... that will come out of the pipeline
# then take up the matches using the $matches object, packing them into a little hash table, the hash will work as a hobo object
$result = $files.name |% {$_ -imatch ".+-(\d\d)\.(\d\d)\.(\d\d\d\d)\..+" | `
Out-Null; @{ "date" = (get-date -Day $Matches[1] -Month $Matches[2] -Year $Matches[3] ); "filename"= $_}}

#select that out, like a boss, find the file you've been dreaming of
$result | select {$_.filename}, @{e={$_.date}; l="date"} | sort date
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s