Windows批处理用户输入变量未在if中设置

时间:2017-03-02 17:53:21

标签: windows batch-file

我有一个程序有以下流程。问题是Windows批处理文件没有正确检查errorlevel并且没有设置KILLSTS值。能告诉我这个程序有什么问题以及如何解决这个问题吗?

var context: JSContext?

private func initJS() {
    context = JSContext()
    if let jsSrcPath = Bundle.main.path(forResource: "script", ofType: "js") {
        do {
            let jsSrcContents = try String(contentsOfFile: jsSrcPath)
            _ = context?.evaluateScript(jsSrcContents)
        } catch let error {
            print(error.localizedDescription)
        }
    }
}

private func getJSVar(name: String) {
    if let vb = context?.objectForKeyedSubscript(name) {
        print("\(vb)")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    initJS()
    getJSVar(name: "pip_buffer")
}

以下是样本批处理文件。

Ask user to open an exe
if Yes
   check exe is running or not
      if running, ask user whether to close that exe
          if yes close exe
   run the exe
else
  exit 

我收到以下错误 enter image description here

2 个答案:

答案 0 :(得分:1)

您需要了解how to properly format if条款。

将它们格式化为:

  

IF / I%KILLSTS%== Y TASKKILL / f / im" STS.exe"

格式化为:

  

if / i"%KILLSTS%" ==" Y" (TASKKILL / f / im STS.exe)

格式化在简单的批处理文件中并不重要,但最好使用正确的语法来处理特殊字符,例如SPACES, AMPERSANDS, QUOTES, PIPE,以便在涉及更复杂的变量时使用


更新了脚本:

@ECHO OFF
@REM SETLOCAL ENABLEEXTENSIONS
    SET /P "AREYOUSURE=Open Spring STS [y/n]>"

    set "AREYOUSURE=%AREYOUSURE:~0,1% "
    echo "AREYOUSURE=%AREYOUSURE:~0,1%"
IF /I "%AREYOUSURE%"=="N" (
    SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
    echo Existing Batch
    EXIT /B %errno%
)
    SETLOCAL
    @REM SET KILLSTS=Y
    tasklist /fi "IMAGENAME eq STS.exe" | find ":" > nul
    ECHO Error %errorlevel%
IF "%errorlevel%" neq "0" (
    call :escapeexpansion
)
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
exit /b
:escapeexpansion
    SETLOCAL
    SET /P "KILLSTS=Spring STS is running. Kill STS Process [y/n]>"
    echo KILLSTS %KILLSTS%
    set "KILLSTS=%KILLSTS:~0,1%"
    echo KILLSTS AFTER SUBSTR %KILLSTS%
    IF /I "%KILLSTS%"=="Y" TASKKILL /f /im "STS.exe"
    ENDLOCAL
goto :EOF

答案 1 :(得分:0)

整个结构对我来说似乎不对;以及使用SET /P代替CHOICE毫无意义地使用。

@ECHO OFF
TASKLIST /FI "IMAGENAME eq STS.exe"|FIND ":">NUL 2>&1&&GOTO ASKIF
CHOICE /M "Spring STS is running. Kill STS Process"
IF ERRORLEVEL 2 GOTO ENDIT
TASKKILL /F /IM "STS.exe"
TIMEOUT 3 /NOBREAK>NUL

:ASKIF
CHOICE /M "Open Spring STS"
IF ERRORLEVEL 2 GOTO ENDIT
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"

:ENDIT
Echo=Exiting Batch
TIMEOUT 3 /NOBREAK>NUL