locked
Find/Replace Extended Character Set characters in filenames in one pipeline RRS feed

  • Question

  • Hello all,

    I have to work with some very bored people. Instead of putting a dash (hex 2d) into a filename, they opt for something from this set of extended characters, which makes my regular expressions fail.  IS there a way I can efficiently find & replace anything outside the standard character set in one pipelinewithout finding and replacing a character at a time?

    So,I'd like something like:

    get-childitem * | where-object $_.name -match '\x99' | rename-item -newname { $_.name -replace '\x99','='}

    from hex 80 to hex FF rather than a for-each.

    Thanks.

    Saturday, February 15, 2014 3:49 PM

Answers

  • Answer would depend on the way you want to replace... Easier if you want replace any char in set with selected char:

    $Name = -join (180..190|%{[char]$_})
    New-Item -ItemType File -Name $Name
    
    Get-ChildItem * | Rename-Item -NewName { 
        [regex]::Replace(
            $_.Name, 
            '[\xB4-\xBE]',
            '_'
        )
    } -WhatIf
    

    But if you want it more complicated, you may do that too. E.g. defining hashtable that can be used to replace individual elements:

    $Replacer = @{}
    
    foreach ($Char in (180..190 | % { [char]$_ })) {
        $Replacer.Add(
            [string]$Char,
            (echo _, -, =, . | Get-Random)
        )
    }
    
    $Replacer
    
    Get-ChildItem * | Rename-Item -NewName { 
        [regex]::Replace(
            $_.Name, 
            '[\xB4-\xBE]',
            {
                $Replacer[$args[0].Value]
            }
        )
    } -WhatIf
    

    Using this syntax make it possible to include some logic in replace. E.g. you could easily use switch to decide what to do with given string:

    Get-ChildItem * | Rename-Item -NewName { 
        [regex]::Replace(
            $_.Name, 
            '[\xB4-\xBE]',
            {
                switch ($args[0].Value) {
                    º { "0" }
                    · { "." }
                    µ { "u" }
                    ¹ { "1" }
                    ¸ { "," }
                    Default { "_" }
    
                }
            }
        )
    } -WhatIf
    

    Sunday, February 16, 2014 1:48 PM

All replies

  • Answer would depend on the way you want to replace... Easier if you want replace any char in set with selected char:

    $Name = -join (180..190|%{[char]$_})
    New-Item -ItemType File -Name $Name
    
    Get-ChildItem * | Rename-Item -NewName { 
        [regex]::Replace(
            $_.Name, 
            '[\xB4-\xBE]',
            '_'
        )
    } -WhatIf
    

    But if you want it more complicated, you may do that too. E.g. defining hashtable that can be used to replace individual elements:

    $Replacer = @{}
    
    foreach ($Char in (180..190 | % { [char]$_ })) {
        $Replacer.Add(
            [string]$Char,
            (echo _, -, =, . | Get-Random)
        )
    }
    
    $Replacer
    
    Get-ChildItem * | Rename-Item -NewName { 
        [regex]::Replace(
            $_.Name, 
            '[\xB4-\xBE]',
            {
                $Replacer[$args[0].Value]
            }
        )
    } -WhatIf
    

    Using this syntax make it possible to include some logic in replace. E.g. you could easily use switch to decide what to do with given string:

    Get-ChildItem * | Rename-Item -NewName { 
        [regex]::Replace(
            $_.Name, 
            '[\xB4-\xBE]',
            {
                switch ($args[0].Value) {
                    º { "0" }
                    · { "." }
                    µ { "u" }
                    ¹ { "1" }
                    ¸ { "," }
                    Default { "_" }
    
                }
            }
        )
    } -WhatIf
    

    Sunday, February 16, 2014 1:48 PM
  • Wow! Thank you Bartek.  This is just perfect. I apologize for not acknowledging this sooner.  Long day at slave labor camp (job).  I actually saw your solution in a powershell quiz ("powershell".getenumerator).  It iddn't occur to me to use a hash table as a mechanism.

    Thank you.

    Thank you.

    Thank you.

    Tuesday, February 18, 2014 8:13 AM