繁体   English   中英

批处理脚本:for /F 执行命令后什么也不做

[英]Batch script : for /F does nothing after executing a command

此脚本的目标是显示计算机保存的网络中的每个 WiFi 设备及其密钥。

预期输出:

<device_name1> : <key>
<device_name2> : <key>
<device_name3> : <key>
<device_name4> : <key>

这是当前脚本:(它只显示设备和键的前 4 个字符)

@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

电流输出:

Free : aaaa
TP-L : aaaa
Bbox : aaaa
...

我猜该变量未在命令中正确应用,并且没有任何循环。 但是,我已经将此语法用于其他脚本,它应该可以工作。

我很迷茫寻找为什么这不起作用T^T


  • 命令netsh wlan show profiles name=!_profile_name! key=clear | find "Key Content" netsh wlan show profiles name=!_profile_name! key=clear | find "Key Content" netsh wlan show profiles name=!_profile_name! key=clear | find "Key Content"在第一个循环中有效,但我无法与密钥交互(下面的示例)
Free :
    Key Content            : v6v...
TP-L :
    Key Content            : 844...
Bbox :
    Key Content            : 7pf...
...

(使用的脚本)

@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
  • 对于“for /F”循环(和其他选项),我已经尝试过使用和不使用 usebackq
  • 与第一个循环内的:getKey部分相同
  • 使用和不使用 EnableDelayedExpansion 时相同(并使用 %var%、!var!、%1、%%B、%%K、...)

我希望我不只是犯了一个愚蠢的错误。 这个项目并不重要,所以请不要花费/浪费太多时间来解决我的问题。

感谢@Hackoo 在我原始问题的评论中解决了我的问题。

我只是没有在命令netsh wlan show profiles name=%1 key=clear ^| find "Key Content"中转义= netsh wlan show profiles name=%1 key=clear ^| find "Key Content"

如果有人想要,这是最终脚本:

@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

以下批处理文件代码可用于在运行英文 Windows 的 PC 上获取 WLAN 密钥:

@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在不在双引号参数字符串中的情况下,用FOR集中的普通空格替换所有逗号、分号、等号和不间断空格。 出于这个原因,也有必要用^转义第三组FOR中的所有等号以按字面意思解释它们。 此代码甚至适用于包含空格、冒号或感叹号的 wlan 网络名称和安全密钥。

要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • find /?
  • for /?
  • netsh /?
  • netsh wlan /?
  • netsh wlan show /?
  • netsh wlan show profiles /?
  • setlocal /?

阅读有关使用命令重定向运算符的 Microsoft 文档,了解2>nul| . 重定向运算符>| 必须在FOR命令行上使用脱字符^进行转义,以便在 Windows 命令解释器在执行命令FOR之前处理此命令行时被解释为文字字符, %SystemRoot%\System32\cmd.exe /c使用在后台启动的单独命令进程执行嵌入式命令行%SystemRoot%\System32\cmd.exe /c'中的命令行作为附加参数附加。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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