简体   繁体   中英

How do you set a specific part of the output of a CMD batch command as a variable? or alternatively, just echo it?

I'm writing a simple little batch file that gets the password of a saved Wi-Fi network, but I want to grab the Key Content, then paste it on its own. Here's the current code:

@echo off
set /p name=Enter Wi-Fi Name:
cls
echo %name%
netsh wlan show profile name="%name%" key=clear 
cmd /k

This gives me a long list of data, but the line I'm looking for is the "Key Content" line. What I essentially want to do is grab the "Key Content" line, clear all the lines, then echo the "Key Content" line. Is this possible without any plugins on Windows 11?

I'm new to the site and coding as a whole, by the way, so what may seem like something completely obvious to you is something I have a 95 percent chance not to know. Thank you!

Here's a snippet, based upon you having received and properly validated the end users input:

@For /F "Tokens=1,* Delims=:" %%H In ('
 %SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%name%" Key^=Clear
 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do @(Set "}=%%I" & SetLocal EnableDelayedExpansion
    For %%J In ("!}:~1!") Do @EndLocal & Echo(%%J)

You should always perform robust validation of any end users input, especially when using the Set /P command, which can accept nothing or absolutely anything, (including malicious content) . If the target systems are Windows 8 / Server 2012 or above, then I wouldn't waste time asking the end user to self-determine the wireless profile name, and then type it correctly at a prompt. I'd just output all of the profile names, along side their returned keys.

The following examples are untested, and assume that your 'passwords' do not include double-quote characters. They should provide the output in a CSV-like format, ("ProfileName","Password") :

@For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
 /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name
 /Format:MOF 2^>NUL') Do @For /F "Tokens=1,* Delims=:" %%H In ('
 %SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%%G" Key^=Clear
 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do @(Set "}=%%I" & SetLocal EnableDelayedExpansion
    For %%J In ("!}:~1!") Do @EndLocal & Echo("%%G",%%J)
@Pause

As you appear to have used the tag, despite your question being about a , you could probably do similarly, directly in the Command Prompt, (cmd.exe) too:

For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name /Format:MOF 2^>NUL') Do @For /F "Tokens=1,* Delims=:" %H In ('"%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name="%G" Key=Clear 2>NUL | %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: ""') Do @Set "}=%I" & For /F Delims^=^ EOL^= %J In ('%SystemRoot%\System32\cmd.exe /V /D /C "Echo "%G","!}:~1!""') Do @(Set /P ="%J") 0<NUL & Echo(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM