在cmd中运行的命令所产生的输出与在for循环中进行解析时产生的输出不同

时间:2018-12-23 16:41:04

标签: batch-file for-loop

我正在尝试使用for /f循环访问我的网络安全密钥。我用来获取它的命令(以cmd为单位)如下:

netsh wlan show profiles name="NETWORK NAME" key=clear

我认为这并不重要,但是网络名称包含两个单词,并用空格分隔。

我对此进行了几次尝试:

1。。尝试通过一种简单的方法对其进行解析:

for /f "tokens=4" %%A IN ('netsh wlan show profiles name="NETWORK NAME" key=clear ^| findstr /c:"Key content"') do echo %%A

这没有任何输出,所以我尝试了:

for /f "tokens=4" %%A IN ('netsh wlan show profiles name="NETWORK NAME" key=clear') do echo %%A

向我展示了

parameters
[[name=]<string>]
of
of
display
data
interface
is
profile
is
listed.
will
set
be
are
preference
1"
2"
3"

具有delims=选项的是:

One or more parameters for the command are not correct or missing.
Usage: show profiles [[name=]<string>] [interface=<string>] [key=<string>]
Parameters:
    Tag             Value
    name          - Name of the profile to display.
    interface     - Name of the interface which has this profile configured.
    key           - To display the key in plain text, set key=clear.
Remarks:
    Shows the profile data or lists the profiles on the system.
    Parameter name and interface are both optional.
    If profile name is given then the content of the profile will be
    displayed. Otherwise only profile name and description will be listed.
    If interface name is given, only the specified profile on the given
    interface will be listed. Otherwise, all profiles with the given name
    on the system will be listed.
    If key is set to "clear" and the caller is local administrator,
    the key will be shown in plain text.
    Group Policy Profiles are read only. User Profiles are readable and
    writeable, and the preference order can be changed.
Examples:
    show profiles name="profile 1" interface="Wireless Network Connection"
    show profiles name="profile 2"
    show profiles name="profile 3" key=clear
    show profiles

2。。使用usebackq

for /f "usebackq delims=" %%A IN (`netsh wlan show profiles name="NETWORK NAME" key=clear`) do echo %%A

与上述相同的输出...

3。。将网络名称设置为引用的变量:

set network="NETWORK NAME"
for /f "delims=" %%A IN ('netsh wlan show profiles name=%network% key=clear') do echo %%A

即使使用usebackq,输出也与上述相同。

4。。将网络名称设置为不带引号的变量:

set network=NETWORK NAME
for /f "delims=" %%A IN ('netsh wlan show profiles name="%network%" key=clear') do echo %%A

即使使用usebackq,输出也与上述相同。

我在这里想念什么?

1 个答案:

答案 0 :(得分:5)

您必须转义一些特殊字符。 =是其中之一:

for /f "delims=" %%A IN ('netsh wlan show profiles name^="NETWORK NAME" key^=clear') do echo %%A

其他必须转义的字符为:<>|&,)