'Batch script : for /F does nothing after executing a command
The goal of this script is to display every WiFi devices with their keys, from the computer's saved networks.
expected output :
<device_name1> : <key>
<device_name2> : <key>
<device_name3> : <key>
<device_name4> : <key>
This is the current script : (it displays only the first 4 characters of both the devices and the keys)
@echo off
setlocal EnableDelayedExpansion
goto :main
:getKey
for /F "tokens=2 delims=: usebackq" %%B in (`netsh wlan show profiles name=%1 key=clear ^| find "Key Content"`) do (
set _clear_key=%%B
)
goto :eof
:main
for /F "tokens=2 delims=:" %%A in ('netsh wlan show profiles ^| find "User Profile"') do (
set _profile_name=%%A
set _profile_name=!_profile_name%:~1!
set _clear_key=aaaaaaa
call :getKey !_profile_name!
echo !_profile_name%:~0,4! : !_clear_key%:~0,4!
)
pause
goto :eof
Current output :
Free : aaaa
TP-L : aaaa
Bbox : aaaa
...
I guess that the variable isn't correctly applied in the command and nothing is looped. However, I already used this syntax for other scripts and it should work.
I'm quite lost searching why this doesn't work T^T
- The command
netsh wlan show profiles name=!_profile_name! key=clear | find "Key Content"
works inside the first loop, but I can't interact with the key (Example below)
Free :
Key Content : v6v...
TP-L :
Key Content : 844...
Bbox :
Key Content : 7pf...
...
(script used)
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=2 delims=:" %%A in ('netsh wlan show profiles ^| find "User Profile"') do (
set _profile_name=%%A
set _profile_name=!_profile_name%:~1!
echo !_profile_name%:~0,4! :
netsh wlan show profiles name=!_profile_name! key=clear | find "Key Content"
)
pause
- I already tried with and without usebackq for both "for /F" loops (and the other options)
- same with the
:getKey
part inside the first loop - same with and without EnableDelayedExpansion (and using %var%, !var!, %1, %%B, %%K, ...)
I hope I haven't just made a dumb error. This project is not important so, please don't spend/waste too much time trying to solve my problem.
Solution 1:[1]
My issue has been solved thanks to @Hackoo in the comments of my original question.
I just didn't escape the =
in the command netsh wlan show profiles name=%1 key=clear ^| find "Key Content"
Here's the final script if anyone wants it :
@echo off
setlocal EnableDelayedExpansion
goto :main
:getKey
for /F "tokens=2 delims=: usebackq" %%B in (`netsh wlan show profiles %1 key^=clear ^| find "Key Content"`) do (
set _clear_key=%%B
set _clear_key=!_clear_key:~1!
)
goto :eof
:main
for /F "tokens=2 delims=:" %%A in ('netsh wlan show profiles ^| find "User Profile"') do (
set _profile_name=%%A
set _profile_name=!_profile_name:~1!
set _clear_key=No password
call :getKey !_profile_name!
echo !_profile_name:~0! : !_clear_key:~0!
)
pause
exit /b
Solution 2:[2]
The following batch file code can be used to get the WLAN key on PCs running English Windows:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1* delims=:" %%G in ('%SystemRoot%\System32\netsh.exe wlan show profiles 2^>nul ^| %SystemRoot%\System32\find.exe "User Profile"') do for /F "tokens=*" %%I in ("%%H") do for /F "tokens=1* delims=:" %%J in ('%SystemRoot%\System32\netsh.exe wlan show profiles name^="%%I" key^=clear ^| %SystemRoot%\System32\find.exe "Key Content"') do for /F "tokens=*" %%L in ("%%K") do echo Key for wlan %%I is: %%L
endlocal
cmd
replaces all commas, semicolons, equal signs and no-break spaces by normal spaces in set of FOR on not being within a double quoted argument string. For that reason it is necessary to escape also all equal signs in set of third FOR with ^
to interpret them literally. This code works even for wlan network names and security keys containing a space, a colon or an exclamation mark.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
find /?
for /?
netsh /?
netsh wlan /?
netsh wlan show /?
netsh wlan show profiles /?
setlocal /?
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul
and |
. The redirection operators >
and |
must be escaped with caret character ^
on FOR command line to be interpreted as literal characters when Windows command interpreter processes this command line before executing command FOR which executes the embedded command line in a separate command process started in background with %SystemRoot%\System32\cmd.exe /c
and the command line within '
appended as additional arguments.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Luna Rezyia |
Solution 2 |