Home > Net >  Input from users for batch script
Input from users for batch script

Time:03-30

I am new to this so if I have put this in the wrong area or done something wrong, please let me know what I need to do to fix it.

During lockdown, I started messing about with Batch to create a menu system to automatically connect my NAS drives. Got to tinkering and added a whole host of other things to the menu system. My latest wee project is for a batch file to show what wifi SSID you are connected to and show the user the SSID password in plain text.

My question is regarding grabbing user input from a batch script. I currently have the following :

:WIFI
CLS
@echo off
@echo  =======================================================================================================
@echo.
@echo Current SSID in range
netsh wlan show networks | findstr SSID
echo.
@echo  Please type in your current connected WiFi name 
set /p  SSID="SSID: "
echo your SSID is: %SSID%
@echo.
@echo  =======================================================================================================
@echo.
PAUSE
CLS
goto SSID

:SSID
@echo  =======================================================================================================
@echo.
@echo  Current connected WiFi and connection password is :
@echo.
NETSH WLAN SHOW PROFILE %SSID% KEY=CLEAR | findstr Name
NETSH WLAN SHOW PROFILE %SSID% KEY=CLEAR | findstr Key 
@echo.
@echo  =======================================================================================================
@echo.
PAUSE
CLS
GOTO RETURN

When I run the script, the first part comes back as the following Output snap

The script works absolutely fine, however, I am looking for a way to be able to have the user just hit the relevant number by the SSID, rather than have to type things out. Does anyone have any ideas?

Failing that, does anyone have any ideas on how to get the batch to automatically show the SSID that is connected to and then run the second part of the script?

CodePudding user response:

Use choice command

setlocal enabledelayedexpansion
for /f "tokens=1-3*delims=: " %%i in ('netsh wlan show networks ^| findstr "SSID"') do (
    set "count=!count!%%j"
    set "SSID%%j=%%k
    echo %%j: %%k
)
choice /c %count% /m "Choose an SSID:"
set "SSID=!SSID%errorlevel%!"
echo your SSID is: %SSID%

The above code will replace the following lines:

netsh wlan show networks | findstr SSID
echo.
@echo  Please type in your current connected WiFi name 
set /p  SSID="SSID: "
echo your SSID is: %SSID%

CodePudding user response:

Here is to get to which network you are connected:

@echo OFF
setlocal 
set "current=no network"
for /f "tokens=2 delims=: " %%a in ('netsh wlan show all ^| find " SSID  "') do set "current=%%a"
echo your are currently connected to %current%.
  • Related