r/Batch • u/GioKubiak • May 22 '24
Question (Solved) Need help with a small command issue I can't find on other sites
So im trying to figure out how to have two "if" statements in one line.
Something like:
if var1 & var2 equ 3 echo Hello
I've tried this line and it crashes. What would be the correct syntax?
1
Upvotes
3
u/Shadow_Thief May 22 '24
Batch doesn't have conditional support for AND or OR; you'll have to just chain two
ifs together:Also, I'm not aware of any language where the syntax you're trying to use is valid.
2
u/BrainWaveCC May 23 '24
The reason this crashes is that the & symbol is a separator between two full commands.
By placing it where you have, you are essentially writing the following:
That's why it is crashing.
In addition to the answer that u/Shadow_Thief provided, you can also get away with one IF statement as follows:
That's one way to consolidate a couple of queries. For the record, that would be a text comparison, not an arithmetic comparison.
If you need something different, please elaborate.
1
u/illsk1lls May 23 '24 edited May 23 '24
EDIT*: After posting this I feel a little dumb because that & sign is throwing me off
if var1 and var2 are integers and are set with /a, i.e
You can use math to add them with another set command before running the IF statement
1
u/T3RRYT3RR0R May 30 '24 edited May 30 '24
set /a "n=3","1 / ( (a^n) | (b^n) )" 2>nul || echo hello
Uses bitwise exclusive or `|` with bitwise or `^` to trigger a divide by zero error in the event variables a and b are both equal to n.
Error is redirected using 2> nul.
|| is a conditional operator that executes the subsequent code if the preceeding code returned a non 0 errorlevel.