Windows 创建UI以通过批处理半自动化托管网络设置

Windows 创建UI以通过批处理半自动化托管网络设置,windows,batch-file,networking,adhoc,Windows,Batch File,Networking,Adhoc,我有一些UI批处理代码,用于重新配置和/或启动托管网络。我调试它有困难。if语句不起作用。请参阅下文: @ECHO OFF REM Provide user interface. REM Ask user if he would like to: REM 1. Re-Configure hosted network REM i. Collect data for hosted Network. REM ii. Provi

我有一些UI批处理代码,用于重新配置和/或启动托管网络。我调试它有困难。if语句不起作用。请参阅下文:

@ECHO OFF
REM Provide user interface.
REM     Ask user if he would like to:

REM         1. Re-Configure hosted network
REM             i.  Collect data for hosted Network.
REM             ii. Provide status message letting user know if setup was successful.

REM         2. Start Hosted Network
REM             i.  Provide Status message (Success or Failure).

REM         3. Use timer to close application after completion.
:Begin

CLS
COLOR 0A

ECHO Please select one of the following options:
ECHO    1. Re-Configure your hosted network.
ECHO    2. Start your hosted network.
ECHO    3. EXIT
ECHO.

SET /P choice=Choice: 

IF "%choice%" EQU 1
    (
        GOTO :config
    )
ELSE IF "%choice%" == 2
    (
        GOTO :startUp
    )
ELSE IF "%choice%" == 3
    (
        GOTO :exit
    )
ELSE
    (
        CLS
        COLOR 4E

        ECHO INVALID SELECTION. PLEASE TRY AGAIN.

        TIMEOUT /t 5
        GOTO :Begin
    )

:config
ECHO Please enter the following information:
SET /P sSID=SSID/Network Name: 
SET /P key=Network Password : 
SET /P start = Start Network (Y/N)?

NETSH wlan SET hostednetwork mode=allow ssid="%sSID%" key="%key%" | FIND "        *successfully*"
NETSH wlan SHOW hostednetwork

IF /I "%start%"=="Y" GOTO :startUP

GOTO :exit

:startUp
NETSH wlan START hostednetwork | FIND "The hosted network started."
GOTO :exit

:exit
ECHO Exiting
FOR /L %Counter IN (0,1,9) DO
    (
        TIMEOUT /t 1000
        ECHO .
    )

pause
请注意,我使用的是windows 8.1


另外,如果有人添加代码来授予文件管理员权限,并向我展示如何从find语句导入if语句,如果出现问题,if语句将显示错误消息,我将不胜感激。

if命令首先失败,因为括号错误。此外,在变量周围有引号,但在值周围没有引号。最后,在GOTO中,不要在标签前使用

某些set命令会在变量中添加空格,这可能会在使用变量时导致失败。删除了空格。通常要注意额外的空格(无回显),尤其是在SET命令中

我添加了评估find命令结果的方法。它统计包含该字符串的行数,如果result=0,则未找到该字符串——失败。有多种方法可以使用管理员权限运行文件,但不在批处理中使用代码

@echo off
    REM Provide user interface.
    REM     Ask user if he would like to:

    REM         1. Re-Configure hosted network
    REM             i.  Collect data for hosted Network.
    REM             ii. Provide status message letting user know if setup was successful.

    REM         2. Start Hosted Network
    REM             i.  Provide Status message (Success or Failure).

    REM         3. Use timer to close application after completion.
:Begin
    CLS
    COLOR 0A
            ECHO Please select one of the following options:
            ECHO    1. Re-Configure your hosted network.
            ECHO    2. Start your hosted network.
            ECHO    3. EXIT
            ECHO.
        SET /P choice=Choice: 
            IF %choice% EQU 1 (
                GOTO config
            ) ELSE (
                IF %choice% EQU 2 (
                GOTO startUp
            ) ELSE (
                IF %choice% EQU 3 (
                GOTO exit
            ) ELSE (
                CLS
                COLOR 4E
                ECHO INVALID SELECTION. PLEASE TRY AGAIN.
                TIMEOUT /t 5
                GOTO Begin
            )))

:config
        ECHO Please enter the following information:
        SET /P "sSID=SSID/Network Name:" 
        SET /P "key=Network Password:" 
        SET /P "start=Start Network (Y/N)?"
        :: Confirm user typed y or n
            If /i not %start%==y echo.>nul &&If /i not %start%==n echo You didn't type y or n &&goto config
        REM NOTE: WILL USER EVER TYPE "N"? If yes, you know they get back here per goto at end (endless loop)

        rem Set var only because length of command
        Set NETSHwlan=NETSH wlan SET hostednetwork mode=allow ssid="%sSID%" key="%key%"
            For /f %%I in ('%NETSHwlan%^| FIND "*successfully*" /c') do (
                If %%I GTR 0 (
                    echo Success
                ) else (
                    echo Failure
                )
            )

        NETSH wlan SHOW hostednetwork
        IF /I "%start%"=="Y" GOTO startUP 
        GOTO config

:startUp
    For /f %%I in ('NETSH wlan START hostednetwork^| FIND "The hosted network started." /c') do (
            If %%I GTR 0 (
                echo Success
            ) else (
                echo Failure
            )
    )
    GOTO exit

:exit
    ECHO Exiting
        FOR /L %Counter IN (0,1,9) DO (
            TIMEOUT /t 1000
            ECHO.
            )
    pause
    exit
请参阅:config中关于无止境循环的REM注释

我认为你的柜台不行。我不知道你想做什么。搜索会发现一些有效的方法