Batch file 批处理文件中的Y或N语句
我是一个涉猎批处理文件的人,所以我的知识仅限于我的经验。我想做的是将“Y或N”输入限制为Y或N。现在,您可以在字段中输入任何内容,代码也可以继续进行。我试图做的是使用批处理文件创建一个热点。我还没有弄清楚如何“保存”创建的网络,但这不是一个真正的问题 我已经包括了我所拥有的,这些行是开始和结束,如果有人碰巧看到任何可以改进或使其不那么笨重的东西,请随意评论Batch file 批处理文件中的Y或N语句,batch-file,Batch File,我是一个涉猎批处理文件的人,所以我的知识仅限于我的经验。我想做的是将“Y或N”输入限制为Y或N。现在,您可以在字段中输入任何内容,代码也可以继续进行。我试图做的是使用批处理文件创建一个热点。我还没有弄清楚如何“保存”创建的网络,但这不是一个真正的问题 我已经包括了我所拥有的,这些行是开始和结束,如果有人碰巧看到任何可以改进或使其不那么笨重的东西,请随意评论 @echo off :: BatchGotAdmin :------------------------------------- REM
@echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
@echo off
:SSID
set /P inputA="Input desired Network SSID:"
echo.
set /P c=Is %inputA% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :PSWD
if /I "%c%" EQU "N" goto :SSID
:PSWD
set /P inputB="Input desired 8 to 63 character Network Password:"
echo.
set /P c=Is %inputB% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :SETUP
if /I "%c%" EQU "N" goto :PSWD
:SETUP
netsh wlan set hostednetwork mode=allow ssid=%inputA% key=%inputB% >NUL
@echo Creating Network...
echo.
timeout /t 5 /nobreak > NUL
@echo Network Created!
echo.
timeout /t 1 /nobreak > NUL
set /P c=Would you like to start your new Network? [Press "Y" to continue/Press "N" to abort]
if /I "%c%" EQU "Y" goto :START
if /I "%c%" EQU "N" goto :BYE
:START
netsh wlan start hostednetwork
timeout /t 5 /nobreak > NUL
@echo Your Network has started!
pause
:BYE
Exit
不要使用
set/p
,而是使用choice
命令。一、 就个人而言,将使用:
choice /m Correct?
if %errorlevel% equ 1 goto PSWD
if %errorlevel% equ 2 goto SSID
这将显示:是否继续?[是/否]?
。如果点击y,它将转到:PSWD
。如果他们点击n,它将转到:SSID
choice命令的帮助部分(由choice/?
在命令提示符中显示)
你的问题的答案可以很容易地通过谷歌的快速搜索找到。请下次再做更多的研究。@MatthewHorvath,由于我在这方面的教育(我在网上找到的零碎的自学/改编代码,但不理解),相关信息不容易识别。我就是这样自学的。这不容易,我知道你来自哪里。但是,你可以尝试在谷歌中使用不同的术语,或者使用命令字典(我个人使用ss64.com/nt)。谢谢你的帮助,撇开斯奈德的评论不谈,非常感谢。
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."