20 秒后自动进行选择

Automatically make a selection after 20 seconds

我为我的 Minecraft 服务器制作了一个自定义启动脚本,这样我就可以使用更多 RAM。 当您停止服务器时,它会询问您是要重新启动还是要停止。 如果你在 20 秒后没有回答,我无法让它自动重启 (以防崩溃)。请帮忙。

@echo off
title minecraft-server-1.8.3
color 0A
prompt [server]:
cls

:start                                          //starts the server
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui 
cls

[code to restart the server when it didn't get an anwser in 20sec]

:choice                                         //what to do when 
set /P a=do you want to restart[Y/N]?             the server stops
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
goto :choice


:restart                                               
cls
echo server will restart
TIMEOUT /T 5
cls
goto :start

:stop

cls
echo closing server
TIMEOUT /T 5
exit

而不是 set /p 使用 choice:

choice /c YN /t 20 /d Y /m "Do you want to restart "
  • /c YN 表示答案集(Y 或 N,顺便说一下不区分大小写)。
  • /t 20 表示超时为 20 秒。
  • /d Y 表示超时到期后的默认答案 Y。
  • /m "..." 表示提示。

choiceerrorlevel 设置为输入的索引 (Y:1, N:2) 因此要检查用户输入使用:

if %errorlevel% equ 1 goto :restart
if %errorlevel% equ 2 goto :stop

您可以在 http://ss64.com/nt/choice.html 或键入 choice/?

找到有关 choice 的更多信息