Using “SET /P” inside an IF statement

2019-04-30 00:47发布

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

5条回答
成全新的幸福
2楼-- · 2019-04-30 01:04

Not sure what your trouble is, this simplified version of your code runs just fine for me:

@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

This is my output -- the batch file was named '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.
查看更多
放荡不羁爱自由
3楼-- · 2019-04-30 01:08

While you shouldn't have any spaces between the switch and the equal sign, or the equal sign and the parameter, the real culprit is because you're using SET /P inside the IF statement.

To correct this, you'll need to do two things:

  1. Add Setlocal EnableDelayedExpansion to the top of your batch file, after the @ECHO OFF statement (so that the variables in the IF block can be expanded at execution time).

  2. Since we're now using EnableDelayedExpansion, call all your variables using !! instead of %%, such as:

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

查看更多
Fickle 薄情
4楼-- · 2019-04-30 01:09
@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]!"
)

The result of calling the function will be:

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

In this way, we can call the function with parameters, use the parameters in another part of the function, and iterate using new inputs from SET.

查看更多
闹够了就滚
5楼-- · 2019-04-30 01:11

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
查看更多
啃猪蹄的小仙女
6楼-- · 2019-04-30 01:17

Here is another way to do it. I also removed the spaces around ssid= and key= as that may be an issue.

@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
查看更多
登录 后发表回答