1.    31 Jul 2008 #1
    JMinnick Guest

    Replacing Multiple Characters In A String


    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 System SpecsSystem Spec

  2.    31 Jul 2008 #2
    Brandon Shell [MVP] Guest

    Re: Replacing Multiple Characters In A String


    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 System SpecsSystem Spec

  3.    31 Jul 2008 #3
    Kiron Guest

    Re: Replacing Multiple Characters In A String


    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 System SpecsSystem Spec

  4.    31 Jul 2008 #4
    Kiron Guest

    Re: Replacing Multiple Characters In A String


    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 System SpecsSystem Spec

  5.    31 Jul 2008 #5
    JMinnick Guest

    Re: Replacing Multiple Characters In A String


    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 System SpecsSystem Spec


Similar Threads
Thread Forum
VBScript String Replace - Remove / Replace Characters in String
VBScript String Replace http://www.code-tips.com/2009/04/vbscript-string-clean-function-remove.html Remove or replace specific characters from...
VB Script
Remove characters in a string
Hello I've got a string ->ScriptName = wscript.Scriptfullname and the actually path is c:\testfolder\myprogram\script.vbs I'm wanting to...
VB Script
Select first 16 characters in a string
I need to figure out how to take a string and pick out the first 16 characters of that string to be used in a pattern match later. Anyone have any...
PowerShell
Replacing String Value in Array does not work
I exported eventlog of one of my server using wmi and I am trying to replace the value of timegenerated in the array after converting it to datetime...
PowerShell
How to Randomize characters in a string
I have a string of of characters that are being stored in a variable. I would like to randomize those characters. How can I do that? Rick
VB Script
Capture a string of characters and use as filename.
I have hundreds of Excel files created using Powershell with generic filenames. The first Cell (A,1) contains infomation I would like to use as...
PowerShell
Removing characters from a string
Hi there, I have a variable, $test, that contains the full path to a file on my machine. What I wish to accomplish is remove everything from the...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 20:07.
    .