none
replacing multiple "replaces" with a cleaner/shorter version RRS feed

  • Question

  • Hi,

    I have a script to clean up the names of files in a directory, removing rubbish entries.  It runs something like this.

    $newfile = $newfile -replace "-abc"

    $newfile = $newfile -replace "-def"

    $newfile = $newfile -replace "-ghi"

    $newfile = $newfile -replace "-jkl"

    The so, a file called "test -abc.txt" becomes test.txt, and a file called "test2 -abc -def.txt" becomes test2.txt

    However, I'm sure there's a shorter, more elegant way of doing this, (there always is with PS).

    e.g. I'd like to set up some form of input and array, something like.

    variable = "abc", "def", "ghi"

    foreach $entry in $variable { $newfile = $newfile - replace $entry"}

    Unless I can simply pipe multiple entries directly into the replace?

    thanks everyone,

    Gavin

    Tuesday, January 21, 2014 8:33 AM

Answers

  • Nope, they're just different languages. Backticks are for escaping characters in PowerShell double-quoted strings. Regular Expressions, on the other hand, use backslashes.

    • Marked as answer by A G Williams Tuesday, January 21, 2014 9:30 PM
    Tuesday, January 21, 2014 4:21 PM

All replies

  • You can specify multiple patterns inside a single regular expression, if this is going to just be hard-coded. If you want to do this dynamically, then your approach of looping over an array also has potential.

    $newfile = $newfile -replace "-(abc|def|ghi|jkl)"

    Tuesday, January 21, 2014 1:19 PM
  • You can specify multiple patterns inside a single regular expression, if this is going to just be hard-coded. If you want to do this dynamically, then your approach of looping over an array also has potential.

    $newfile = $newfile -replace "-(abc|def|ghi|jkl)"

    that's great!

    and what about if I want to delete brackets... it doesn't seem to quite work when I do the following.

    INPUT:

    $dir = dir | where {!$_.psiscontainer}
    $dir.fullname
    foreach ($file in $dir)
    {
        $newfile = $file.basename -replace '[.]', " "
        $newfile = $newfile -replace "-(abc|def|ghi|jkl|(|))"
        "renaming "+$file.BaseName + " into $newfile"
        rename-item $file.name $newfile
    }

     OUTPUT:

       

    T:\Downloads\Test1 (-abc).txt

    T:\Downloads\test2-abc ().txt

    T:\Downloads\testbase.txt

    T:\Downloads\testc-abc.txt

    renaming Test1 (-abc) into Test1 ()

    renaming test2-abc () into test2 ()

    renaming testbase into testbase

    renaming testc-abc into testc

    Tuesday, January 21, 2014 3:16 PM
  • Parentheses are special characters in the regular expression language. They need to be escaped with backslashes inside the expression.

    Also, the pattern would need to be slightly tweaked; by placing the parentheses where you did, they would only be replaced if they immediately followed a hyphen. Here's what I think you wanted to do:

    $newfile = $newfile -replace "-abc|-def|-ghi|-jkl|\(|\)"

    • Proposed as answer by Mike Laughlin Tuesday, January 21, 2014 4:39 PM
    Tuesday, January 21, 2014 3:25 PM
  • That's great... it did the clean-ups.  Thanks so much

    one final question....

    I thought the "escape" was the backtick ' ... but you used a backslash.  Are they interchangeable?

    Tuesday, January 21, 2014 4:18 PM
  • Nope, they're just different languages. Backticks are for escaping characters in PowerShell double-quoted strings. Regular Expressions, on the other hand, use backslashes.

    • Marked as answer by A G Williams Tuesday, January 21, 2014 9:30 PM
    Tuesday, January 21, 2014 4:21 PM