提示询问问题2次。我怎样才能解决这个问题?

时间:2019-07-22 20:58:47

标签: batch-file

我试图检查eula.txt中的EULA是否设置为true,但是当键入true时,它将再次询问,我必须再次键入true。

我在Windows 10上尝试使用.cmd文件而不是.bat(同一件事)

@echo off
SET jarFileName=server.jar
SET jarFileUrl=https://cdn.getbukkit.org/spigot/spigot-1.12.2.jar
SET memory=4G
SET startColor=0F
SET errorColor=CF

rem Colors and things
color %startColor%
MODE CON COLS=136 LINES=36

rem Start server
:checkEULA1
if exist eula.txt (
goto checkEULA2
) else (
echo eula=false>eula.txt
goto checkEULA2
)

:checkEULA2
>nul findstr /c:"eula=true" eula.txt && (
  rem EULA true
  goto start
) || (
  rem EULA not true
  echo Do you accept the EULA? Type true if you do.
  set /p eula=
  echo eula=%eula% >eula.txt
  goto checkEULA1
)

:start
pause
if exist %jarFileName% (
    java -version >nul 2>&1|| cls&&color %errorColor%&&echo Please install Java and add it to PATH. Usually the 64 bit JDK works better. && goto exit
    cls
    java -Xmx%memory% -jar %jarFileName% nogui
) else (
    cls
    echo %jarFileName% not detected. Downloading...
    powershell -Command "(New-Object Net.WebClient).DownloadFile('%jarFileurl%', '%jarFileName%')"
    echo Done!
    echo.
    echo Press any key to start server... && pause>nul && cls && goto start
)

rem Exit
:exit
echo.
color %errorColor%
echo.
echo Press any key to exit... && pause>nul && exit

我希望它只问一次问题。我看不到任何错误。

2 个答案:

答案 0 :(得分:1)

我认为您可能可以这样做更简单一些:

@Echo Off
Set "startColor=0F"
Set "errorColor=CF"

Set "jarFileName=server.jar"
Set "jarFileUrl=https://cdn.getbukkit.org/spigot/spigot-1.12.2.jar"
Set "memory=4G"

FindStr /I "^eula=true$" "eula.txt" >NUL 2>&1||(Color %errorColor%
    Choice /M "Do you accept the EULA"
    If ErrorLevel 2 Exit /B 1
    (Echo eula=true)>eula.txt)

ClS
Mode 136,36
Color %startColor%
Rem Start server

答案 1 :(得分:0)

问题是您的eula变量仅在退出findstr函数之后设置。

一个简单的解决方案是创建另一个函数并goto

:checkEULA1
    if exist eula.txt (
        goto checkEULA2
    ) else (
        echo eula=false>eula.txt
        goto checkEULA2
    )

:checkEULA2
    findstr /c:"eula=true" "eula.txt" >nul 2>&1 && (
        rem EULA true
        goto start
    ) || (
        rem EULA not true
        set /p eula= Do you accept the EULA? Type true if you do: 
        goto setEULA
    )

:setEULA
    echo eula=%eula%>eula.txt
    goto checkEULA1