如何麻烦Windows选择循环?

时间:2018-08-02 02:52:33

标签: loops batch-file variables if-statement

setlocal EnableDelayedExpansion
:adapterloopstart
echo.
echo please enter the type of adapter you would like to create
echo 1. Host-Only
echo 2. Internal
echo.

set ADAPTYPE=
choice /C:12
if ERRORLEVEL 2 set ADAPTYPE=intnet
goto adapterconfirm
if ERRORLEVEL 1 set ADAPTYPE=hostonly
goto adapterconfirm
REM If you got here, it wasn't 1, 2
goto adapterloopstart
echo %ADAPTYPE%

:adapterconfirm
echo %ADAPTYPE%
set confirmadapter=
set /p confirmadapter=Do you wish to create a "%ADAPTYPE%" network (y,n):
if not '%confirmadapter%'=='' set confirmadapter=%confirmadapter:~0,1%
if '%confirmadapter%'=='y' goto adapterend
if '%confirmadapter%'=='n' goto adapterloopstart
ECHO "%choice%" is not valid, try again
:adapterend

上面的代码给出以下字符串:

  

您是否要创建“”网络(y,n):

它不显示变量。

如何解决此问题,所以我知道它以正确的选择保存了变量,以便以后我希望重新运行此代码时可以得到正在发生的事情的反馈?

1 个答案:

答案 0 :(得分:0)

我认为这将简化您的逻辑。使用choice命令使您受益。

@echo off
:adapterloopstart
cls
echo.
echo Please enter the type of adapter you would like to create
echo 1. Host-Only
echo 2. Internal
echo.

set "ADAPTYPE="
choice /C:12
if ERRORLEVEL 1 set "ADAPTYPE=hostonly"
if ERRORLEVEL 2 set "ADAPTYPE=intnet"

choice /M "Do you wish to create a '%ADAPTYPE%' network:"

if errorlevel 2 goto adapterloopstart

:adapterend