Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace characters in string before file extension

Tags:

powershell

I've got a variable, with the following string values:

PS C:\> $sql.physicalname
I:\Data\Data1.mdf
I:\Data\Data2.ndf
I:\Data\Data3.ndf
I:\Data\Data4.ndf
I:\Data\Data5.ndf
I:\Data\Data6.ndf
I:\Data\Data7.ndf
I:\Data\Data8.ndf
I:\Data\Data9.ndf
I:\Data\log.ldf

What I want is to replace/trim the strings so that only the .mdf/.ndf/.ldf extensions remain:

.mdf
.ndf
.ndf
.ndf
.ndf
.ndf
.ndf
.ndf
.ndf
.ldf

I tried using the -replace parameter with the * wildcard, but I cant wrap my head around it.

like image 399
Smeerpijp Avatar asked Dec 04 '25 17:12

Smeerpijp


1 Answers

As it is a regular expression you pass to -replace, try it e.g. with

-replace ".*\.", "."  

A better and more robust way would be to use something like that

$sql.physicalname | % { [System.IO.Path]::GetExtension($_) }
like image 148
DAXaholic Avatar answered Dec 06 '25 13:12

DAXaholic