Batchfile - 命令的语法不正确

时间:2016-05-07 16:26:50

标签: windows batch-file windows-7 syntax-error

我是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的内容只是一个零。 (&#34; 0&#34;没有引号)

1 个答案:

答案 0 :(得分:2)

您的问题是变量!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