批处理文件IF语句不会解析(比较两个整数变量)

时间:2018-01-05 01:21:31

标签: batch-file cmd

我正在尝试创建一个小批处理程序,它会在检测到程序没有运行时重新启动程序,并且还会重新启动程序。

这是CMD刚刚说的“命令的语法不正确”的部分(在第一个if语句之后不会解析)。两个变量都使用/ a语句设置,因此它们都应该是正确的整数。

IF %delayer% GTR %resetDelay% (

IF NOT %resetDelay% EQU 0 (
  CALL :RESETMINER
  ECHO [%TIME%] %minerProcess% - scheduled restart completed.
  SET /a delayer=0
) ELSE (
  TIMEOUT %checkDelay%
)

) ELSE (

SET /a delayer=%delayer%+%checkDelay%
TIMEOUT %checkDelay% /nobreak
::Delete /nobreak if you want to skip the 'Wait' sequences by pressing any key

)

这是整个代码

    @ECHO off
::SETLOCAL EnableDelayedExpansion
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit ) &::For testing purposes only

COLOR a
set VERSION=1.0
TITLE Miner controller v%VERSION%

::USER SETTINGS
SET minerProcess=ccminer.exe   &:: Process name of your miner software (you can find it out in the Task Manager)
SET /a checkDelay=10           &:: Delay (approx) between checks (can be low) in seconds
SET /a resetFreq=1             &:: Delay (approx) between miner restarts in minutes (0=never)
                                :: Don't forget to change the START command with your miner settings
::END OF USER SETTINGS

ECHO [%TIME%] Miner controller v%VERSION% started
SET /a delayer=0

IF NOT %resetFreq%==0 (
  SET /a resetDelay=%checkDelay%*60*%resetFreq%
) ELSE (
  SET /a resetDelay=0
)

:MAIN

  TASKLIST | FIND /i "%minerProcess%" && ECHO [%TIME%] %minerProcess% running OK. || CALL :startminer
  SET /a restart=(%resetDelay%-%delayer%)/60

  IF NOT %resetDelay% EQU 0 ECHO Scheduled restart in %restart%min.

  IF %delayer% GTR %resetDelay% (

    IF NOT %resetDelay% EQU 0 (
      CALL :RESETMINER
      ECHO [%TIME%] %minerProcess% - scheduled restart completed.
      SET /a delayer=0
    ) ELSE (
      TIMEOUT %checkDelay%
    )

  ) ELSE (

    pause
    SET /a delayer=%delayer%+%checkDelay%
    TIMEOUT %checkDelay% /nobreak
    ::Delete /nobreak if you want to skip the 'Wait' sequences by pressing any key

  )

  GOTO MAIN

EXIT /B %ERRORLEVEL%


:STARTMINER

  ECHO [%TIME%] Starting %minerProcess%
  ::REPLACE WITH YOUR MINER CONFIG
  START ccminer -o stratum+tcp://etn-eu.cryptominingpool.eu:5556 -u etnk8sNSUV9FBGnFbKnWYigeoAD13ECSBh34CcJEkdnggnobYykQr9G4KkosnCVdSoRgfW1GuX5uoBi9bB7frAsy9gfAWAp438 -p x

EXIT /B


:RESETMINER

  ::SET /a delayer=0
  ECHO [%TIME%] Killing miner...
  TASKLIST | FIND /i "%minerProcess%" && taskkill /im %minerProcess% /F || ECHO [%TIME%] Can't kill %minerProcess% - not running.
  CALL :STARTMINER

EXIT /B

1 个答案:

答案 0 :(得分:0)

不要在代码块(带括号的行序列)中使用::-comments,因为它会导致问题。将其更改为rem comment