簡體   English   中英

Windows批處理編程-目前無法預期

[英]Windows batch programming - Unexpected at this time

我只是在嘗試一些基本的批處理編程。 執行期間出現錯誤

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" 

我在錯誤

設置/ a“ x = 0”

0此時是意外的。

我在這里做錯了什么?

約20億次delayedexpansion

在塊語句(a parenthesised series of statements) ,將分析整個然后執行。 解析該塊時-執行該塊之前-該塊內的任何%var%都將由該變量的值替換-同樣的情況也適用於FOR ... DO (block)

因此,將在遇到IF時使用%variables%的值執行IF (something) else (somethingelse)

解決此問題的兩種常用方法是:1)使用setlocal enabledelayedexpansion和使用!var! 代替%var%訪問的改變值var或2),以調用一個子程序使用改變的值來執行進一步的處理。

因此-輕松解決:

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

筆記:

GOTO 不需要在標簽上一個冒號,除了在特殊情況:EOF其含義是指end of file

SET /一個不需要引號。

SET“ stringname = stringvalue”是用於字符串分配的良好語法,因為引號導致該行上的所有尾隨空格都不包含在分配的值中。

循環內變化的任何%var%都應變為!var! delayedexpansion以訪問run-time不是parse-time時值。

(我只解決了延遲擴展錯誤-REM標記了其他問題)

在我看來,該錯誤是由括號內的“ CALL”引起的。 而不是在IF語句中使用括號塊,而應將括號中的代碼放在外部“ GOTO方法”中。 在dos批處理編程中,必須注意括號。 我個人已經開發了很少需要的代碼風格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM