windows - 批处理文件 - 命令的语法不正确

标签 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 的内容只是一个零。 (“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

关于windows - 批处理文件 - 命令的语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37090948/

相关文章:

c# - 在 Android 中转换现有的 Windows 10 UWP 应用程序

c++ - 我应该如何在 Win32 C++ 应用程序中集成和打包此第三方库?

windows - & 和 | 是做什么的运算符(operator)在批处理文件中做什么?

windows - 如何创建一个批处理脚本,在达到文件夹大小限制时删除最旧的文件

windows-7 - Windows 使用 .ico 的每个分辨率版本有何用途? (例如,开始菜单、桌面)?

windows-7 - 没有默认 DHCP 服务器的 ICS

c++ - 执行 writefile 函数两次

windows - .gitconfig 安装在 Windows 10 的错误驱动器上

batch-file - 使用批处理文件编辑文本文件

c# - 如何在 MS WIndows 7 中获取音频设备的插孔信息