8

I'm creating a batch to turn my laptop into wifi an make my life easier to type lines in cmd each time.

The trouble is the wifi name always get set to key= insted of the one which I enter.

Here is what I did:

@echo OFF
set /p option="Enter 1 to create wifi, Enter 2 to stop wifi "
IF %option% EQU 1 (
    set /p id="Enter wifi Name:"
    set /p key="Set password:"
    netsh wlan set hostednetwork mode=allow ssid = %id% key = %key%
    netsh wlan start hostednetwork
)

IF %option% EQU 2 (
netsh wlan set hostednetwork mode=disallow
)

timeout /t 5
4

5 回答 5

13

虽然您不应该在开关和等号或等号和参数之间有任何空格,但真正的罪魁祸首是因为您在语句SET /P内部使用。IF

要纠正这个问题,您需要做两件事:

  1. 在语句之后添加Setlocal EnableDelayedExpansion到批处理文件的顶部@ECHO OFF(以便IF可以在执行时扩展块中的变量)。

  2. 由于我们现在使用,请使用而不是EnableDelayedExpansion调用所有变量,例如:!!%%

    netsh wlan set hostednetwork mode=allow ssid=!id! key=!key!

于 2013-07-11T19:13:14.067 回答
3

Got the solution

@echo off

echo What You What To Do ?
echo 1 to create wifi
echo 2 to stop wifi

set /p input=
if %input%==1 goto 1
if %input%==2 goto 2

:1
cls
set /p name=Enter wifi name 
set /p pass=Enter wifi password 
echo Creating wifi with
echo Name = %name% 
echo Password = %pass%
netsh wlan set hostednetwork mode=allow ssid="%name%" key="%pass%"
netsh wlan start hostednetwork
timeout /t 5
exit;

:2
cls
netsh wlan set hostednetwork mode=disallow
exit;
timeout /t 5
于 2013-07-11T19:50:25.047 回答
1
@echo off
setlocal enabledelayedexpansion

SET /P myvar="Enter variables: "

set argCount=0
for %%x in (%myvar%) do (
   set /A argCount+=1
   set "argVec[!argCount!]=%%~x"
)

echo Number of processed arguments: %argCount%

for /L %%i in (1,1,%argCount%) do (
    echo %%i- "!argVec[%%i]!"
)

调用该函数的结果将是:

function.bat
Enter variables: a b c
1- "a"
2- "b"
3- "c"

这样,我们可以使用参数调用函数,在函数的另一部分使用参数,并使用来自 SET 的新输入进行迭代。

于 2017-04-27T15:36:07.513 回答
0

这是另一种方法。我还删除了 ssid= 和 key= 周围的空格,因为这可能是一个问题。

@echo OFF
set "option="
set /p "option=Enter a name to create wifi, or just press Enter to stop wifi: "

IF not defined option (
netsh wlan set hostednetwork mode=disallow
goto :EOF
)

    set /p key="Set password:"
    netsh wlan set hostednetwork mode=allow ssid=%option% key=%key%
    netsh wlan start hostednetwork

timeout /t 5
于 2013-07-12T20:22:06.447 回答
-1

不知道你的问题是什么,你的代码的这个简化版本对我来说运行得很好:

@echo OFF
set /p option="Enter 1 to create wifi, Enter 2 to stop wifi "
IF %option% EQU 1 (
    echo Option 1
)

IF %option% EQU 2 (
    echo Option 2
)

timeout /t 5

这是我的输出——批处理文件名为“z.bat”

C:\z>z
Enter 1 to create wifi, Enter 2 to stop wifi 1
Option 1

Waiting for 2 seconds, press a key to continue ...

C:\z>z
Enter 1 to create wifi, Enter 2 to stop wifi 2
Option 2

Waiting for 4 seconds, press a key to continue ...

C:\z>
C:\z>cmd /version
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
于 2013-07-11T19:50:42.873 回答