zask
zask

Reputation: 181

grab batch text content and put it in a variable

okay i have this command

netsh wlan show profiles

pretend that the wireless network that appears is named Zask.router

the prompt should appear something like this;

..........................................................

Profiles on interface wifi:

group policy profiles (read only)

<none>

user profiles

All User Profile     : Zask.router

..........................................................

is there any way i can get the content "Zask.router" to be placed in a variable?

okay thanks, new question. i changed the code to this;

for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "profile=%%A"

now i added this to the next line;

netsh wlan show profiles "%profile%" key=clear

then something that says "key content" (must be administrator) should appear with a wifi password next to it, how do i get that password to also go into a variable? i tried doing something like this but it just displays the word "such" for some strange reason...

for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "profile=%%A
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles "%profile%" key=clear') do set "profile2=%%A"
echo %profile2%

Upvotes: 1

Views: 70

Answers (1)

RGuggisberg
RGuggisberg

Reputation: 4750

Assuming that the output you want is on the last line...

for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "YourVariable=%%A"

Use the "FOR /?" command to see help for FOR command. We are using it to split your lines into tokens separated by either spaces or :

  • tokens=4 specifies that we want to save the 4th token
  • delims=:(space) specifies that the characters ":" and " " are delimiters
  • YourVariable is being set for every line in the output of the NETSH command. In this case, since we only care about the last line it works out without the need to compare text to find the desired line.

Upvotes: 2

Related Questions