rconn
Administrator
- May
- 13,642
- 253
Staff member
There are two undocumented bugs / (unfeatures?) in the IF command in CMD.EXE.
CMD's behavior with IF is to ignore everything else on a command line if the IF fails. This is undocumented, which is one reason I consider it a "bug" -- the second being that it's senseless to treat something following a command separator (&) as being somehow syntactically related to the previous command. (Why not have FOR ignore compound commands if it fails??) If the user wants to evaluate the IF result conditionally, they should be using the && or || operators.
Unfortunately, an number of batch files written for CMD rely on this behavior (sometimes as a unnecessary hack, and sometimes because the author apparently didn't know any better).
The second "unfeature" is when IF is used in a multiline command group. CMD assembles a command group into a single compound command by adding a command separator (&) before each line. The inconsistency is that it treats an end of line within a command group as (slightly) different from a & -- but only for IF! (It resets its internal flag that would otherwise have it ignore the remainder of the line).
CMD's behavior with IF is to ignore everything else on a command line if the IF fails. This is undocumented, which is one reason I consider it a "bug" -- the second being that it's senseless to treat something following a command separator (&) as being somehow syntactically related to the previous command. (Why not have FOR ignore compound commands if it fails??) If the user wants to evaluate the IF result conditionally, they should be using the && or || operators.
Unfortunately, an number of batch files written for CMD rely on this behavior (sometimes as a unnecessary hack, and sometimes because the author apparently didn't know any better).
The second "unfeature" is when IF is used in a multiline command group. CMD assembles a command group into a single compound command by adding a command separator (&) before each line. The inconsistency is that it treats an end of line within a command group as (slightly) different from a & -- but only for IF! (It resets its internal flag that would otherwise have it ignore the remainder of the line).