1

I am just trying out some basic batch programming. I am getting some error during execution

set /a x=0
:while1
if %x% leq 5 (
    echo %x%
    goto:callfun
    /dir:/Prod /user:admin /pass:anc /level:error >> C:\Logs\A.txt
   set file=C:\Logs\J_FINANCIALS_EVENING.log
   set /a "cnt=0"
   for /f %%a in ('type "%file%"^|find "!Kitchen.Error.NoRepDefinied!" /i /c') do set /a cnt=%%a
     if %cnt% NEQ 0 (
          if %x% NEQ 5 (
              DEL C:\Logs\abc.txt
              )
      set /a x=x+1
      goto :while1

)
    echo "OUTSIDE LOOP"
   echo The Status is %errorlevel%
  call:check_file
  exit /b %errorlevel%

)
  endlocal

callfun: 
CALL C:\Pentaho\pdi-ce-5.2.0.0-209\data-integration\kitchen.bat /rep:"Admin" /job:"XXXX" 

I am getting the error at

set /a "x = 0"

0 was unexpected at this time.

What am I doing wrong here?

5
  • Remove any and all spaces on either side of = symbols. Your first line does not set x to 0, but sets the variable x to the string ` 0` (note the spaces). Also, near the bottom you have set /d - there is no /d option. You want /a for math. Commented Nov 21, 2014 at 15:48
  • I edited the code above and still getting the same error. Also code exits at if NEQ 0 (. Commented Nov 21, 2014 at 16:03
  • You need delayed expansion. stackoverflow.com/a/20854972/2152082 explains that. Commented Nov 21, 2014 at 16:36
  • @SomethingDark - The space issue is only valid for normal SET. It does not apply to SET /A (space before and after = is ignored) Commented Nov 21, 2014 at 21:39
  • Good to know. It's still good practice to not have any. Commented Nov 21, 2014 at 21:43

2 Answers 2

2

Episode about 2 billion of delayedexpansion

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

hence - easy fix:

SETLOCAL ENABLEDELAYEDEXPANSION
set /a x=0
:while1
if %x% leq 5 (
    echo !x!
    goto callfun

    REM this following line appear to make no sense in winbatch

    /dir:/Prod /user:admin /pass:anc /level:error >> C:\Logs\A.txt
   set "file=C:\Logs\J_FINANCIALS_EVENING.log"
   set /a cnt=0
   for /f %%a in ('type "%file%"^|find "!Kitchen.Error.NoRepDefinied!" /i /c') do set /a cnt=%%a
     if !cnt! NEQ 0 (
          if !x! NEQ 5 (
              DEL C:\Logs\abc.txt
              )
      set /a x=x+1
      goto while1

)
    echo "OUTSIDE LOOP"
   echo The Status is !errorlevel!
  call :check_file
  exit /b !errorlevel!

)
  endlocal

REM Note that this will fall-through to the process. Best add
GOTO :EOF
REM Here.

REM Colon must precede label
:callfun 
CALL C:\Pentaho\pdi-ce-5.2.0.0-209\data-integration\kitchen.bat /rep:"Admin" /job:"XXXX" 
REM Note that this would exit the subroutine by reaching (apparent) EOF. Best add
GOTO :EOF
REM Here - as a habit - it contributes to preventing fall-through failures
REM if you add a new subroutine and forget to include the newly-required goto :eof

Notes:

GOTO does not require a colon on the label except in the special case of :EOF which is defined to mean end of file

SET /a does not need quotes.

SET "stringname=stringvalue" is good syntax for string-assignments because the quotes cause any trailing spaces on the line to not be included in the value assigned.

any %var% which is varied within the loop should become !var! with delayedexpansion to access the run-time rather than the parse-time value.

(I've only tackled the delayed-expansion errors - other problems are flagged with REMs)

Sign up to request clarification or add additional context in comments.

2 Comments

But quotes are often useful with SET /A when using any of !, <<, >>, &, ^, | operators (also assignment variants). Without quotes the operators must be escaped.
@dbenham : ah, yes - true, but in nearly 20 years of writing NT-style scripts, I've never felt the urge to use those operators. Or maybe I've contrived to avoid them. Good point.
0

To me, it looks like the error is caused by having a "CALL" within a parenthesis block. Instead of using a parenthesis block inside a IF statement, put the code that is in the parenthesis in a external "GOTO method" instead. You have to be careful with parenthesis in dos batch programming. Personally, I have developed a code style that rarely needs them.

1 Comment

Sorry, I forgot to emphasize that anytime , in a batch file, when you have parenthesis inside parenthesis, then your getting yourself into trouble. Try re-writing the script without using any parenthesis and I bet it works. You should ONLY use parenthesis when you are sure that what you are doing isn't going to be problematic to the DOS parser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.