It's not clear what you are inputting.
If you execute the command test1 t1
then:
value
will acquire the value t1
(from the command-line tail)
param
will acquire the value t1
errorlevel
will be set to 0
as the set
was successful, so
if %errorlevel%==0 goto:dif
will go to :dif
as the contents of errorlevel
(%errorlevel%
) is 0
and 0==0
There is a small difficulty here, since if errorlevel
was not 0
, then the if
condition would not execute goto dif
(the colon is better omitted) so the procedure will continue - to the label :dif
in any case...
if %value%==!%param%! call test2.bat
will be evaluated by substitution of the current values and execute
if t1==!t1! call test2.bat
since both value
and param
contain t1
.
BUT t1
is not equal to !t1!
, so the call
instruction will be ignored.
If you execute the command test1
value
will acquire the value nothing (since there is no command-line tail)
param
will acquire the value t1
errorlevel
will be set to 0
as the set
was successful, so
if %errorlevel%==0 goto:dif
will go to :dif
as the contents of errorlevel
(%errorlevel%
) is 0
and 0==0
if %value%==!%param%! call test2.bat
will be evaluated by substitution of the current values and execute
if ==!t1! call test2.bat
since value
contains nothing and param
contains t1
.
This is a syntax error because cmd
expects
if string1==string2 ...
The other issue is the use of !something!
.
Normally, !
is just another character, like x
or q
in batch, and %var%
means the content of the variable "var"
.
When you have a code block ( a series of commands within parentheses) then batch first substitutes the value of var
for %var%
and then executes the command, so
set "var=original"
for %%a in (1 2 3) do (
rem this is a code block
echo before %var%
set "var=modified"
echo after %var%
)
echo then %var%
will report original
, original
3 times and modified
because what is actually executed is
set "var=original"
for %%a in (1 2 3) do (
rem this is a code block
echo before original
set "var=modified"
echo after original
)
echo then %var%
This changes when the special command
setlocal enabledelayedexpansion
is executed. !
then acquires a special meaning.
set "var=original"
setlocal enabledelayedexpansion
for %%a in (1 2 3) do (
rem this is a code block
echo before %var% !var!
set "var=modified"
echo after %var% !var!
)
echo then %var% !var!
endlocal
echo final %var% !var!
In this case, the report will be
before original original
after original modified
before original modified
after original modified
before original modified
after original modified
then modified modified
final original !var!
because %var%
refers to the original value, and !var!
to the modified value (hence at the very first before
, the variable has not yet been modified, but at the after
, the variable has been modified, so the second and third loops report the modified value as !var!
)
After the loop, both %var%
and !var!
report the same thing (the value was mofified within the loop, but not after the end of the loop).
And finally, the endlocal
turns the delayedexpansion
off, so the !
becomes a normal character again, and endlocal
also throws away any modifications made to the variables after the setlocal
was executed. Hence the original value of var
is shown (as it was when the setlocal
was executed) and !var!
is no longer a special string.
So - your
if %value%==!%param%! call test2.bat
would, if setlocal enabledelayedexpansion
had been executed and param
contains t1
actually execute
if the value contained in "value"==the value contained in "t1" call test2.bat