2016-08-23 24 views
1

我是批處理文件中的一員,所以我的知識僅限於我的經驗。我想要做的就是將「Y或N」輸入限制爲Y或N.現在,您可以將任何內容放入字段中並且代碼進度。我試圖做的是使用批處理文件創建一個熱點。我還沒有想出如何「保存」創建的網絡,但這不是一個真正的問題。批處理文件中的Y或N語句

我已經包括了我所擁有的,線條是開始和結束,如果有人碰巧看到任何可以改進的地方,或者減小體積,可以隨意發表評論。

@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 
+6

[選擇](http://ss64.com/nt/choice.html) – Stephan

+0

答案的幫助部分你的問題很容易找到一個快速的谷歌搜索。請下次再做更多的研究。 –

+0

@MatthewHorvath隨着我在這方面的教育(零散的自我教學/適應性代碼,我在網上找到,並不明白)相關的信息不容易識別。 – Red

回答

1

而不是使用set /p,請使用choice命令。我個人會使用:

choice /m Correct? 
if %errorlevel% equ 1 goto PSWD 
if %errorlevel% equ 2 goto SSID 

這將顯示:Continue? [Y/N]?。如果命中y,它將轉到:PSWD。如果他們打到n,它將會轉到:SSID

選擇命令(通過choice /?在命令提示符長大)

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." 
+0

謝謝你的幫助,撇開旁邊的評論,非常感謝。 – Red

相關問題