• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Replacing Multiple Characters In A String

J

JMinnick

#1
I created a powershell script to create directories and rename files based on
MP3 tags..

MP3 tags occasionally have characters the can't be used in filenames/folders..

My solution looks a bit ugly.. Is there a more efficient way to replace all
the special characters in a string than this?

---------------------------------------------------------------------------------

((((((((((" " + $media.Tag.Album).Trim()).Replace( ":" , " " )).Replace("?"
, " ")).Replace("/" , " ")).Replace("\" , " ")).Replace("|" ,"
")).Replace("*" , " ")).Replace("<" , " ")).Replace(">" , " ")).Replace('"' ,
"")

---------------------------------------------------------------------------------

Note: $media.Tag.Album would return a string like "Is there A Key?/Are you
there?".

Creating a folder or file with that name would fail..

My solution does remove all the special characters in a string. A couple of
the characters are replaced with nothing rather than a space which was
intentional.

Any suggestions?

Thanks
 

My Computer

B

Brandon Shell [MVP]

#2
look at the -replace operator

It will replace a RegEx with another string

$string -replace "ThisRegEx","WithThisOne"

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

J> I created a powershell script to create directories and rename files
J> based on MP3 tags..
J>
J> MP3 tags occasionally have characters the can't be used in
J> filenames/folders..
J>
J> My solution looks a bit ugly.. Is there a more efficient way to
J> replace all the special characters in a string than this?
J>
J> ---------------------------------------------------------------------
J> ------------
J>
J> ((((((((((" " + $media.Tag.Album).Trim()).Replace( ":" , " "
J> )).Replace("?" , " ")).Replace("/" , " ")).Replace("\" , "
J> ")).Replace("|" ," ")).Replace("*" , " ")).Replace("<" , "
J> ")).Replace(">" , " ")).Replace('"' , "")
J>
J> ---------------------------------------------------------------------
J> ------------
J>
J> Note: $media.Tag.Album would return a string like "Is there A
J> Key?/Are you there?".
J>
J> Creating a folder or file with that name would fail..
J>
J> My solution does remove all the special characters in a string. A
J> couple of the characters are replaced with nothing rather than a
J> space which was intentional.
J>
J> Any suggestions?
J>
J> Thanks
J>
 

My Computer

K

Kiron

#3
PowerShell's Test-Path has an -IsValid switch that will check a path's syntax, if it returns $false then replace the unwanted chars:

# build an alternaton regex pattern with the unwanted chars:
$fChars = ':', '?', '/', '\', '|', '*', '<', '>', '"'
$pat = [string]::join('|', ($fChars | % {[regex]::escape($_)}))

$name = '"Is there A Key?/Are you there?"'
# replace any unwanted char wth a '_' if path is not valid
if (!(test-path $name -isValid)) {$name = $name -replace $pat, '_'}
$name

$name = '"Is there A Key?/Are you there?"'
# replace any unwanted char wth nothing if path is not valid
if (!(test-path $name -isValid)) {$name = $name -replace $pat}
$name

--
Kiron
 

My Computer

K

Kiron

#4
Missed the ' ' for some and nothing for the rest unwanted chars...

# build two alternation regex patterns with the unwanted chars:
$fChars1 = ':', '?', '/', '\', '*', '<', '>'
$fChars2 = '|', '"'
$pat1 = [string]::join('|', ($fChars1 | % {[regex]::escape($_)}))
$pat2 = [string]::join('|', ($fChars2 | % {[regex]::escape($_)}))

$name = '"Is there A Key?/Are you there?"'
# replace the unwanted char wth a ' ' or nothing if path is not valid
if (!(test-path $name -isValid)) {
$name = $name -replace $pat1, ' ' -replace $pat2
}
$name

--
Kiron
 

My Computer

J

JMinnick

#5
Cool. Very interesting.

Thanks for your help!

"Kiron" wrote:

> Missed the ' ' for some and nothing for the rest unwanted chars...
>
> # build two alternation regex patterns with the unwanted chars:
> $fChars1 = ':', '?', '/', '\', '*', '<', '>'
> $fChars2 = '|', '"'
> $pat1 = [string]::join('|', ($fChars1 | % {[regex]::escape($_)}))
> $pat2 = [string]::join('|', ($fChars2 | % {[regex]::escape($_)}))
>
> $name = '"Is there A Key?/Are you there?"'
> # replace the unwanted char wth a ' ' or nothing if path is not valid
> if (!(test-path $name -isValid)) {
> $name = $name -replace $pat1, ' ' -replace $pat2
> }
> $name
>
> --
> Kiron
>
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)