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?
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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.