1

My code isn't working right, it's supposed to take a floating "this" or "that" and decide if it matches one of the mentioned words, however it is currently stopping at the first if statement and saying all the words match it, which they definitely don't. I've tried -match, -eq, -like .... I placed an "x" in front of the incorrect line. This is only a short line of a much larger code. (BRC - Bulk rename utility(commandline))

if ($this){
    #exceptions
    if ($that -like "Drücker" -or "Biegekern" -or "Führung" -or "Pressentisch"){Write-Host ("x($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"3_$that":"3_$this ($that)" /REPLACECI:_Dr.Pos.:"_$this ($that)_Pos_" /replaceci:" ":_ /execute
    }
    elseif ($that -like "Halteplatte" -or "Oberteil" -or "Unterteil" -or "Schieber"){Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"3_$that":"3_$this ($that)" /replaceci:" ":_ /execute
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"2_$that":"2_$this ($that)" /replaceci:" ":_ /execute
    }
    elseif ($that -like "Schnitt"){Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"0_$that":"0_$this ($that)" /replaceci:" ":_ /execute
    }
    elseif ($that -like "Zusatzführung"){Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"2_$that":"2_$this ($that)" /replaceci:" ":_ /execute
    }

    #main renaming function
    elseif ($that){
        Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"_$that":"_$this ($that)" /replaceci:" ":_ /execute
    }
}
elseif ($that){
    Write-Host ("($i/$rowMax) Blank-Skip")
    $blanks=$blanks+1
}
else {
    $i=$rowMax
}

2 Answers2

2

Meh people make mistakes. I make tonnes! Best way to learn. Since multiple criteria to match $that against something like this might be a better fit. Simple regex with the same logic

if ($that -match "(Drücker|Biegekern|Führung|Pressentisch)"){
    Do-Stuff()
}

If you reacted differently to the matches then a switch would be a better fit.

switch ($that) 
    { 
        Drücker {"wo sind meine Hosen."} 
        Biegekern {"meinem Hologramm ist voller Ohren."} 
        Führung {"mein Deutsch ist nicht gut."} 
        Pressentisch {"Zwei."} 
        default {"keine Ahnung."}
    }

Other more complex examples of switch which would help can be found here. An example using both of the snippets above would be this

$that = "Zusatzführung"

switch -Regex ($that) 
    { 
        "^(Drücker|Biegekern|Führung|Pressentisch)$" {"wo sind meine Hosen."} 
        "^(Halteplatte|Oberteil|Unterteil|Schieber)$" {"meinem Hologramm ist voller Ohren."} 
        "Schnitt" {"mein Deutsch ist nicht gut."} 
        "Zusatzführung" {"Zwei."} 
        default {"keine Ahnung."}
    }

$that in this case would have matched the first entry. If you are looking for complete word matches you could just use ^$ as I have done. You could also use the break keyword to prevent multiple entries from matching.

....
"(Drücker|Biegekern|Führung|Pressentisch)" {"wo sind meine Hosen."; Break} 
"Zusatzführung" {"Zwei."}
....

Using the same $that with the above lines would only match the first. Then after the output line we exit the switch.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Yay for pimping out `Switch`! Such a very under utilized cmdlet imho. – TheMadTechnician Dec 10 '14 at 00:04
  • I wish I could use this solution, but the trouble is I need to match $that to exactly one of those words, and then process to do the exact same thing for all the words of that category. As the list of words gets longer, though, I'm considering making an array of words, but I think I'd run into the same situation as with your code... – joe bredestege Dec 10 '14 at 13:53
  • @joebredestege If you are interested I will take your code and convert it to a swtich statement and you can see it in action. – Matt Dec 10 '14 at 13:57
  • @joebredestege. Have a look at what is there now. – Matt Dec 10 '14 at 14:09
  • you sir, just saved me alot of writing xD But note the syntax is actually break, not return – joe bredestege Dec 11 '14 at 19:09
  • Updated the answer to use the proper keyword – Matt Dec 11 '14 at 19:15
0

Simply put to make this work you need to rewrite -like and -or. I'm just a dingus

if ($that -like "Drücker" -or $that -like "Biegekern" -or $that -like "Führung" -or $that -like "Pressentisch"){Write-Host ("x($i/$rowMax) Replacing $that with $this")
                    .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"3_$that":"3_$this ($that)" /REPLACECI:_Dr.Pos.:"_$this ($that)_Pos_" /replaceci:" ":_ /execute
                }
  • And unless I'm mistaken, the reason is that your first version evaluates to ($that -like "...") -or (...), and all the string literals evaluate to "truthy" values. – Rytmis Dec 09 '14 at 20:11