if [System.IO.Path]::GetFileNameWithoutExtension()
is hard to type or remember:
("file.name.ext.w..dots.ext" -split '\.' | select -SkipLast 1) -join '.'
# >> file.name.ext.w..dots
"file.name.ext.w..dots.ext" -split '\.' | select -Last 1
# >> ext
Notes:
-split
takes a regex (by default) so the .
has to be escaped
I don't think there's a "locale" name.ext filename separator, is there?
-SkipLast
was added in v5.0
The .NET function [System.IO.Path]::GetExtension()
returns the extension including the '.' char; the above returns it without
having to -rejoin the string after splitting could change the result, I suppose, in unusual circumstances. Or if you're uncomfortable re-joining a string that's already joined, one could:
$file = "file.name.ext.w..dots.ext"
$ext = $file -split '\.' | select -Last 1
$name = $file.Substring(0, $file.LastIndexOf(".$ext"))