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

dou*_*eep -1 for-loop batch-file

我试图使用for /f循环访问我的网络安全密钥.我用来获取它的命令(在cmd中)如下:

netsh wlan show profiles name="NETWORK NAME" key=clear
Run Code Online (Sandbox Code Playgroud)

我认为这不重要,但网络名称包括由空格分隔的两个单词.

我对此做了几次尝试:

1.尝试用简单的方法解析它:

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

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

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

这告诉我:

parameters
[[name=]<string>]
of
of
display
data
interface
is
profile
is
listed.
will
set
be
are
preference
1"
2"
3"
Run Code Online (Sandbox Code Playgroud)

哪个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
Run Code Online (Sandbox Code Playgroud)

2.使用usebackq:

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

与上面相同的输出......

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

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

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

4.将网络名称设置为未加引号的变量:

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

输出与上面相同,即使是usebackq.

我在这里错过了什么?

Ste*_*han 5

你必须逃避一些特殊的角色.=是其中之一:

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

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