Alex Lum
Alex Lum

Reputation: 307

POWERSHELL : split a string like awk

In a file i have many lines like this

Nom de l'objet :    C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico

I just want extension of the file (here ico). some extension may have more than 3 characters. With awk, i can do, but in powershell... Thanks for help.

Upvotes: 0

Views: 999

Answers (3)

Mike Shepard
Mike Shepard

Reputation: 18176

Rather than use RegEx, why not use the [System.IO.Path] class to get the extension:

$str = "Nom de l'objet :    C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico"
$filename=($str -split ':',2)[1]
[System.IO.Path]::GetExtension($filename)

Upvotes: 3

Bill_Stewart
Bill_Stewart

Reputation: 24585

$str = "Nom de l'objet :    C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico"
if ( $str.LastIndexOf(".") -gt -1 ) {
    $str.Substring($str.LastIndexOf(".") + 1)
}
# Outputs "ico"

Upvotes: 1

TobyU
TobyU

Reputation: 3918

With some regex:

$str = "Nom de l'objet :    C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico"
$str -match "\.(.{3,})$"
$extension = $Matches[1]
$extension #ico

\.(.{3,})$ matches at least 3 chars after the last dot before the very end of the line.

Upvotes: 2

Related Questions