批量“if”语句,只有在输入文本时才会执行某些操作,否则请执行其他操作

时间:2014-11-08 05:50:59

标签: batch-file input user-input

我有一个具有用户输入的批处理脚本:

set /p serverstart=Starter Path (or leave blank if unknown): 

现在需要做的是,如果用户输入某些内容(任何内容),它将转到特定标签:

if %serverstart% == [anything] goto %serverstart%
:%serverstart%
java -Xmx1024M -Xms1024M -jar %serverstart%.jar nogui

但仅在用户输入内容时。如果用户将该字段留空,则该文件需要继续执行:

set /p serverstart=Starter Path (or leave blank if unknown): 
:serverram
cls
set /p serverram=Server RAM: 
[enter more code here]

因此,如果用户输入内容,则会转到已定义的用户标签,并启动%serverstart%.jar,但如果用户将该字段留空,则不执行任何操作并继续操作。将:

set /p serverstart=Starter Path (or leave blank if unknown): 
if %serverstart% == "" goto %serverstart%
goto serverram
:serverram
set /p serverram=Server RAM: 

:%serverstart%
java -Xmx%serverram%M -Xms%serverram%M -jar %serverstart%.jar nogui

工作?

(只是你想要它,到目前为止是完整的代码)

@ECHO OFF
title MineServer Runner
cls

:servername
cls
if exist ServerFiles\serverconfig.cmd goto startserver
set /p servername=Server Name: 
echo set servername=%servername%>> ServerFiles\serverconfig.cmd
call "ServerFiles\serverconfig.cmd"
title MineServer (%servername%) Runner
set /p serverstart=Starter Path (or leave blank if unknown): 
:serverram
cls
set /p serverram=Server RAM: 
echo set serverram=%serverram%>> ServerFiles\serverconfig.cmd

:startserver
cd ServerFiles
call "serverconfig.cmd"
java -Xmx%serverram%M -Xms%serverram%M -jar starters\minecraft_server.jar nogui

2 个答案:

答案 0 :(得分:1)

if defined serverstart goto (wehaveinput) else (goto wedonthaveinput)

您当然可以使用if not defined serverstart...

请记住 - set您的变量为空(set "var=") before设置/ p`否则它将保持与之前的任何值不变。

答案 1 :(得分:0)

我会转过身来:

if not "%serverstart%" == "" (
  we have input-code
) ELSE (
  we have NO input-code
)

编辑。