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
Not sure what your trouble is, this simplified version of your code runs just fine for me:
This is my output -- the batch file was named 'z.bat'
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 theIF
statement.To correct this, you'll need to do two things:
Add
Setlocal EnableDelayedExpansion
to the top of your batch file, after the@ECHO OFF
statement (so that the variables in theIF
block can be expanded at execution time).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!
The result of calling the function will be:
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.
Got the solution
Here is another way to do it. I also removed the spaces around ssid= and key= as that may be an issue.