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
}
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.