0

I'm newer in bat command and learning .i meet a issue and i can't fix it . it caused a error that "call" was unexpected at this time when i invoke call command behind if judgment.my bat files are below. I ask everyone for help to solve it.thx very much. pls

test1.bat

@echo off
echo test1.bat running
set value=%1
set param=t1

if %errorlevel%==0 goto:dif

:dif
if %value%==%param% call test2.bat

pause

test2.bat

@echo off

echo test2.bat running

it will nothing when i input test1 t1, but the error occur as i input test1 command line in cmd .


Ok,I change the question and update test1.bat file now.

test1.bat

@echo off
echo test1.bat running
set "value=t1"

if  "%value%==-t1"  call "test2.bat"
if  "%value%==t1"   echo ok

There is the same error occured when i input test1 command line.

Error:call was unexpected at this time

I want to know why cause this error.

SnailMann
  • 23
  • 1
  • 8

2 Answers2

0

Here's a quick example of the pertinent lines from your provided scripts:

test1.bat

@Echo Off
Echo %~nx0 running
Set "value=%~1"
Set "param=t1"
If /I "%value%"=="%param%" Call "test2.bat"
Pause

test2.bat

@Echo Off
Echo %~nx0 running

It should show you how to reference the script name dynamically, how to use doublequotes as best practice to protect values or strings and how to do conditional verification using If.

For information on a command, just enter CommandName /? at the Command prompt; i.e. Set /?, If /? or Call /?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • This will only match If full string of `%1` is `t1` and will not match `test1` – Gerhard Jan 31 '18 at 14:42
  • @Gerhard, my reading is that the OP enters `test1 t1` this runs `test1.bat` with the parameter, `t1` as `%1`. They reported an error when they input `test1`, because they ran `test1.bat` with no parameter. Their code therefore `undefined` the variable `%value%` meaning that `if ==!%param%! call test2.bat` was trying to run. – Compo Jan 31 '18 at 14:47
  • Yeah I asked him to clarify now, I am pretty focussed on the last line where he states in the last line _"but the error occur as i input test1 command in cmd"_ so I am also just assuming he wants to match `t1` in `test1` :) – Gerhard Jan 31 '18 at 14:48
  • So we are both almost correct, he wants to match `%2` with `t1` so you can update answer. – Gerhard Jan 31 '18 at 14:55
  • Yes they input the command `test1` in cmd.exe, previous to that they input `test1 t1`. Because `.bat` is included in `%PATHEXT%` it will be run without having to use its extension, otherwise their commands at the Command prompt would have been `test1.cmd t1` and `test1.cmd` – Compo Jan 31 '18 at 14:57
  • @Compo Thanks,It working !But i want to ask you some question too.What is the different between if /t and if? and what is the effects of useing doubleequotes?pls – SnailMann Jan 31 '18 at 14:59
  • ok, I think I got confused with the English usage with OP, so I should rather delete answer. – Gerhard Jan 31 '18 at 14:59
  • @SnailMann, use the `IF /?` as mentioned in my answer, it isn't the letter `t` it is a capitalized letter `i`, which does a case insensitive check meaning that `t1` and `T1` will match! – Compo Jan 31 '18 at 15:02
0

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

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Ok!Thanks for your detail answers,and i got your mean about how to use "setlocal enabledelayedexpansion" command and "!". Thanks.But i want to know why it will report that call was unexpected at this time when i input test1 command line in cmd.exe.Pls ingore ! symbol. – SnailMann Feb 01 '18 at 03:28
  • It's not clear what your *actual* command-entry is. If all you are entering is "test1" then read the second block above, starting `If you execute the command test1` where I've shown what happens step-by-step. Please note that in comments you can change to *italics* using a single asterisk at the beginning and end of the text you want to hilight and **if you use two asterisks then you will get BOLD text** – Magoo Feb 01 '18 at 03:40
  • Ok, I update my question now and the explanation is clearer.pls help to check it and find the casue.thx – SnailMann Feb 01 '18 at 04:52
  • Fine. Now we are certain what is being entered. :) So - please re-read my response, in the second section which starts "If you execute the command `test1`" and ignore the `!` characters as they have now been removed and the saga about `delayedexpansion` is now not relevant. – Magoo Feb 01 '18 at 05:22