我是Minecraft服务器的所有者,目前正在研究制作自动设置脚本的方法,以便其他人可以更轻松地运行服务器。我已经完成了所有工作,但是在测试后,此代码抛出了“命令的语法不正确”错误。这是代码:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
ECHO Welcome to the startup of your new Minecraft server^^!
ECHO Upon completion of the first start, please exit the program and run "start.bat".
ECHO When you are ready, press any key.
PAUSE>NUL
SET /p "hasrun" = < "%~dp0\hasrun.txt"

:Run
IF "!hasrun!" == "0" (
    ECHO "1" >> "%~dp0\hasrun.txt"
    java -Xms1024M -Xmx1024M -jar minecraft_server.jar nogui
) ELSE (
    ECHO This is not your first run^^! Use "start.bat" instead^^!
    PAUSE
)

该错误似乎发生在ECHO "1" >> "%~dp0\hasrun.txt"所在的行。自从我上次批量写东西以来已经有一段时间了,所以这很明显。确切的输出(关闭回声)为:
Welcome to the startup of your new Minecraft server!
Upon completion of the first start, please exit the program and run "start.bat".

When you are ready, press any key.

按下一个键传递PAUSE后,它说:
The syntax of the command is incorrect.
This is not your first run! Use "start.bat" instead!
Press any key to continue...

同样,hasrun.txt的内容只是一个零。 (“0”不带引号)

最佳答案

您的问题是变量!hasrun!。设置变量的方法将使变量为空或无效,如果文件为空,则!hasrun!不等于任何值,这会使for循环失败。

@ECHO OFF
SETLOCAL EnableDelayedExpansion
ECHO Welcome to the startup of your new Minecraft server^^!
ECHO Upon completion of the first start, please exit the program and run "start.bat".
ECHO When you are ready, press any key.
PAUSE>NUL
:: Default variable set, or the for loops fails because "" does not equal "0".
if not exist "%~dp0\hasrun.txt" echo 0 >%~dp0\hasrun.txt
:: Set the file to a variable.
SET /p hasrun=<%~dp0\hasrun.txt

:Run
:: Space after "0 " appended because 0> refers to a debugging echo.
IF "!hasrun!"=="0 " (
    ECHO 1>"%~dp0\hasrun.txt"
    java -Xms1024M -Xmx1024M -jar minecraft_server.jar nogui
    pause
) ELSE (
    ECHO This is not your first run^^! Use "start.bat" instead^^!
    PAUSE
)
del %~dp0\hasrun.txt

10-08 01:16