batch cmd exits as I enter the input with space

if you enter a string with space(s), if syntax will give you a syntax error. Let’s look at:

if hello world == string echo xyz

if syntax is: if <value1> <comparator> <value2> command
So hello is value1, world is the comparator – wait – what? world isn’t a comparator – Syntax error.

Enclose your values in quoutes to be safe:

if "hello world" == "string" echo xyz

So "hello world" is value1, == is the comparator, "string" is value2 and echo xyz is the command. All goes well.

You may be interested in the choice command, which does it’s own error handling and allows only valid keys.

Leave a Comment