如何使用批处理从文本文件中获取特定单词的下一个单词作为输出?

如何使用批处理从文本文件中获取特定单词的下一个单词作为输出?

我已经运行了以下命令行,

netsh wlan show profile MyWiFi-name key=clear >key.txt

输出,key.txt

Profile MyWiFi-name on interface Wireless Network Connection: 
======================================================================= 

Applied: All User Profile    

Profile information 
------------------- 
    Version                : 1
    Type                   : Wireless LAN
    Name                   : MyWiFi-name
    Control options        : 
        Connection mode    : Connect automatically
        Network broadcast  : Connect only if this network is broadcasting
        AutoSwitch         : Do not switch to other networks

Connectivity settings 
--------------------- 
    Number of SSIDs        : 1
    SSID name              : "MyWiFi-name"
    Network type           : Infrastructure
    Radio type             : [ Any Radio Type ]
    Vendor extension          : Not present

Security settings 
----------------- 
    Authentication         : WPA-Personal
    Cipher                 : CCMP
    Security key           : Present
    Key Content            : password@1

我需要的是,我只需要Key Content文本文件中的输出。请帮忙。

例如 :文件中的预期输出key.txt是,

password@1

答案1

我之前编写了一个批处理脚本,用于查找并显示 PC 上注册的所有 SSID 及其密码,然后将其保存在文本文件中。

无线网络密码恢复.bat

@echo off & setlocal enabledelayedexpansion
Set "Copyright=by Hackoo 2017"
Title  %~n0 %Copyright%
Mode con cols=75 lines=8
cls & color 0A & echo.
    echo             ***********************************************
    echo                 %~n0 %Copyright%
    echo             ***********************************************
    echo(
if _%1_==_Main_  goto :Main
Set Count=0
Set L=0
:getadmin
    echo               %~nx0 : self elevating
    set vbs=%temp%\getadmin.vbs
(
    echo Set UAC = CreateObject^("Shell.Application"^)
    echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
goto :eof
::*************************************************************************************
:Main
Call :init
Call :CountLines
Set "PasswordLog=%~dp0Wifi_Passwords_on_%ComputerName%.txt"
%Mod%
    echo(
    echo             ***********************************************
    echo                 %~n0 %Copyright%
    echo             ***********************************************
    echo(
Call :Color 0E "                 [N][SSID] ================ Password" 1
echo(
(
    echo             ***********************************************
    echo                 %~n0 %Copyright%
    echo             ***********************************************
    echo(
    echo                  [N][SSID] ==============^> "Password"
    echo(

)>"%PasswordLog%"
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
    if not "%%a"=="" (
        set "ssid=%%a"
        set "ssid=!ssid:~1!"
        call :Getpassword "!ssid!"
    )
)
echo(
echo Done
If exist "%PasswordLog%" start "" "%PasswordLog%"
pause>nul
exit
::*************************************************************************************
:Getpassword
set "name=%1"
set "name=!name:"=!"
Set "passwd="
for /f "delims=: tokens=2" %%a in ('netsh wlan show profiles %1 key^=clear ^|find /I "Cont"') do (
    set "passwd=%%a"
    Set /a Count+=1
)

If defined passwd (
    set passwd=!passwd:~1!
    echo                  [!Count!][!name!] ====^> "!passwd!"
    echo                  [!Count!][!name!] ====^> "!passwd!" >> "%PasswordLog%"
) else (
    Set /a Count+=1
call :color 0C "                 [!Count!][!name!] The Password is empty" 1
    echo                  [!Count!][!name!] The Password is empty >> "%PasswordLog%"
)
exit /b
::*************************************************************************************
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::*************************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::*************************************************************************************
:CountLines
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
    if not "%%a"=="" (
        set /a L+=1
    )
)
set /a L=!L! + 10
Set Mod=Mode con cols=75 Lines=!L!
exit /b
::*************************************************************************************

输出结果如下: 在此处输入图片描述

答案2

key.txt 文件中的预期输出是

密码@1

使用以下批处理文件(test.cmd):

@echo off
setlocal enabledelayedexpansion
netsh wlan show profile MyWiFi-name key=clear >tmp.txt
for /f "tokens=2 delims=:" %%i in ('type tmp.txt ^| findstr "Key Content"') do (
  set _key=%%i
  set _key=!_key:~1!
  echo !_key! > key.txt
  )
endlocal

例子:

> test

> type key.txt
password@1

进一步阅读

答案3

netsh wlan show profile (Network-Name) key=clear | findstr /C:"Key Content" > key.txt 
FOR /F "tokens=4 delims= " %%i in (key.txt) do @echo %%i > key.txt

所有 key.txt 示例都应路径到您希望包含密钥的文本文件所在的位置。

相关内容