Reputation: 1116
I have a string like this:
\aaaaaaaaa\bbbbb\ccccc\ffffffff20160506
The part before the date is dynamic, so I just can't do a replace or substring - that part can change in both content and number of characters.
Can you please advise how can I extract that date to a variable?
Upvotes: 1
Views: 459
Reputation: 20237
Asuming that the date is always at the end of the string and in yyyyMMdd format:
$foo = "\aaaaaaaaa\bbbbb\ccccc\ffffffff20160506"
$datevar = $foo.substring($foo.length-8)
If you want to cast that string to a DateTime object you can use:
$datevar = [datetime]::ParseExact($datevar, "yyyyMMdd",$null)
Upvotes: 2
Reputation: 711
I am not that into powershell but I guess you can do something like this,
Eventhough the string is dynamic the date part is exactly 8 characters right? So what if you get the full length and subtract 8 characters and get the substring from that index.
Example:
string= \aaaaaaaaa\bbbbb\ccccc\DDDDD_yyyyMMdd length = 37 point=37-8 = 29
subsctring(9,37)
Upvotes: 2