Let me explain about delayed expansion. First, let's look at the context of the problem. In your original code, you had:IF '%var%' == 'cls' GOTO clear
The problem with that is that the single quote is not treated as anything special. It's just another character. You might as well have written:
IF a%var%a == aclsa GOTO clear
Obviously, your intention was to get the IF statement to compare two strings. Normally, the IF statement can compare two words, so you can write this:
IF %var% == cls GOTO clear
This will work only if %var% contains a single word. The % percent-sign operator causes the variable to be replaced by its value before the whole line is parsed. So if var contains the word Fred, the above line will first be expanded to this:
IF Fred == cls GOTO clear
which is perfectly good syntax. It compares two words, they are not equal so it doesn't goto clear.
Now if var contained the phrase "two words" (without the quotes), the above would become:
IF two words == cls GOTO clear
This is a syntax error because after IF the command processor expects to see a word followed by ==. To fix this, you can use double quotes, which makes the parser treat the phrase as a single word:
IF "%var%" == "cls" GOTO clear
which expands to
IF "two words" == "cls" GOTO clear
which again is perfectly valid syntax.
What if var contained the string between the parentheses here: (two" == "words) ?
Now this:
IF "%var%" == "cls" GOTO clear
expands to:
IF "two" == "words" == "cls" GOTO clear
which is a syntax error because it expects a valid command in place of the second ==.
What can we do here? The answer: use delayed expansion. To use delayed expansion, you first have to enable it at the start of your batch file with this command:
setlocal EnableDelayedExpansion
Now, you can use !var! instead of %var%. The difference is that the variable expansion is done after parsing, not before. So our solution to this problem is the following:
IF !var! == cls GOTO clear
The syntax is valid, because it is checked before !var! is expanded. var can in fact contain anything you wish. It can contain multiple words separated by spaces, or commas, or it can contain any punctuation character you like (including quotes.) Whatever it contains, it will be compared with the word cls with the results you expect.
If the right-hand side contains multiple words, you need to use quotes:
IF "!var!" == "multiple words" GOTO clear
Or you can do this:
SET test=multiple words
IF !var! == !test! GOTO clear
I hope this makes sense. Good luck!