Saturday, October 22, 2016

Powershell v2 Regular Expressions Extracting Dates from File Names

,
Been too busy to blog much lately.  On a current work project I have some weird file names in a pattern like this:
o123_123_20120402_ask1.iso
I needed to extract the data for each file for some processing and figured this was a great opportunity for delving into Regular Expressions.  Part of the goal here was to stop looking as Regular Expressions as a curse from an as of yet undisclosed alien language, but, rather, a tool.  An, not only a tool, but, one I can use.  To deal with the Date value from this string I started constructing regexes and settled on this one for my first go round:
"(?<text>.*)(??<data>d{8})(??<text2>.*)"
The regular expression parses a file name, searches for any number of characters and groups this first match into the "text" match.  The "data" group looks for an 8-digit {8} numeric d string.  Finally, it looks for any remaining characters in the line and throws them into a "text2" group.

As I worked on it I came up with this sample snippet.  The regex was explained above.  The second line is for the sample name (just dummy data) against which we match the pattern.  The Out-Null is just to prevent the True or False value from returning.  Finally, the last line is an effort to convert the string to a [DateTime] format MM-dd-yyyy.
$pattern = "(?<text>.*)(?<date>d{8})(?<text2>.*)"
"a_12345678_at.txt" -match $pattern | Out-Null
([DateTime] ($matches.date)).ToString(MM-dd-yyyy)
The error I got was:
Cannot convert value "20120224" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:23
+ ([DateTime] ($matches. <<<< date)).ToString(MM-dd-yyyy) + CategoryInfo : NotSpecified: (:) [], RuntimeException + FullyQualifiedErrorId : RuntimeException
The more I worked with it the less I realized I knew what I was doing, so, I threw a post on the forums.
How can I extract dates from a funky filename (dates in name as yyyyMMdd)
The data was not coming out correctly, and, I couldnt quite figure out how to cast the string ($matches.date) to DateTime.

Kazun struck first with this:
PS > [DateTime]::ParseExact("20120224","yyyyMMdd",$null).ToString(MM-dd-yyyy)
02-24-2012
This worked, and, now, I have a [DateTime] just as I had hoped.  So, I now am going to tattoo [DateTime]::ParseExact on my hand.

0 comments to “Powershell v2 Regular Expressions Extracting Dates from File Names”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger