How to handle If operations?
Just a quick note how to write correct If structures in batch files...
Lets have a look at this line:
If %1 EQU Success Echo Looks fine
Looks fine, right? But you could run into multiple issues:
- If you run it and Variable doesnt exist, you will receive error message "Success was not expected at this time". Reason is that cmd will try to execute command If EQU Success Echo Looks fine, however it is missing one argument.
- If variable is success and not Success, it wont work.
Regarding first issue, common workaroung is to use If "%1" EQU "Success" Echo Looks fine, however this is also not best was how to handle this issue. Problem is if your variable contains " signs. Therefore best way (IMHO) is to use "#". So it should looks like If #%1# EQU #Success# Echo Looks fine.
Regarding second issue, always use "/i" parameter - this means that comparison is not case sensitive.
I personally also recommend to use %~1 at beginning. This means that quotes (") are removed from parameter specified. If you apply quotes AGAIN to this ("%~1"), it means that this parameter is always parsed with quotes (regardless of if they were provided in original parameter). That means that Script.cmd "testing" is the same as Script.cmd testing.
So my line would looks like
If /i #"%~1"# EQU #"Success"# Echo Looks fine
Martin