简体   繁体   English

Windows批处理编程-目前无法预期

[英]Windows batch programming - Unexpected at this time

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" 设置/ a“ x = 0”

0 was unexpected at this time. 0此时是意外的。

What am I doing wrong here? 我在这里做错了什么?

Episode about 2 billion of delayedexpansion 约20亿次delayedexpansion

Within a block statement (a parenthesised series of statements) , the entire block is parsed and then executed. 在块语句(a parenthesised series of statements) ,将分析整个然后执行。 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) . 解析该块时-执行该块之前-该块内的任何%var%都将由该变量的值替换-同样的情况也适用于FOR ... DO (block)

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered. 因此,将在遇到IF时使用%variables%的值执行IF (something) else (somethingelse)

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! 解决此问题的两种常用方法是:1)使用setlocal enabledelayedexpansion和使用!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. 代替%var%访问的改变值var或2),以调用一个子程序使用改变的值来执行进一步的处理。

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 GOTO 不需要在标签上一个冒号,除了在特殊情况:EOF其含义是指end of file

SET /a does not need quotes. SET /一个不需要引号。

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. SET“ stringname = stringvalue”是用于字符串分配的良好语法,因为引号导致该行上的所有尾随空格都不包含在分配的值中。

any %var% which is varied within the loop should become !var! 循环内变化的任何%var%都应变为!var! with delayedexpansion to access the run-time rather than the parse-time value. delayedexpansion以访问run-time不是parse-time时值。

(I've only tackled the delayed-expansion errors - other problems are flagged with REMs) (我只解决了延迟扩展错误-REM标记了其他问题)

To me, it looks like the error is caused by having a "CALL" within a parenthesis block. 在我看来,该错误是由括号内的“ CALL”引起的。 Instead of using a parenthesis block inside a IF statement, put the code that is in the parenthesis in a external "GOTO method" instead. 而不是在IF语句中使用括号块,而应将括号中的代码放在外部“ GOTO方法”中。 You have to be careful with parenthesis in dos batch programming. 在dos批处理编程中,必须注意括号。 Personally, I have developed a code style that rarely needs them. 我个人已经开发了很少需要的代码风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM