为什么命令wifiname
中的变量为空?show profile
FOR /F "tokens=2 delims=:" %%G IN ('"netsh wlan show profiles"') DO (
Set myvar=%%G
SET wifiname=%myvar:~1%
@echo '%%G'
@echo '%wifiname%'
netsh wlan show profile name="%wifiname%" key=clear >> wp.txt
)
如果我%%G
直接传递(不使用双引号),它会排除用空格分隔的多个单词的网络。前导空格将被忽略。
使用双引号,传递完整的网络名称,每个网络名称的前导空格都是问题......
最简单的解决方案 - 删除前导空格%%G
并将结果用双引号传递给最终命令...如何做?
答案1
如何从变量中删除前导空格?
使用以下批处理文件(test.cmd):
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=2 delims=:" %%G IN ('netsh wlan show profiles') DO (
SET _temp=%%G
SET wifiname=!_temp:~1!
netsh wlan show profile name="!wifiname!" key=clear
)
endlocal
示例输出:
> test
Profile virginmedia3954038 on interface Wireless Network Connection:
=======================================================================
Applied: All User Profile
Profile information
-------------------
Version : 1
Type : Wireless LAN
Name : virginmedia3954038
Control options :
Connection mode : Connect automatically
Network broadcast : Connect only if this network is broadcasting
AutoSwitch : Do not switch to other networks
...
答案2
从字面上理解你的问题,有几种方法可以实现这一点。
在你的情况下最简单的是使用一个更好的,For /f
它
也使用空间"delims=: "
假设你有类似的输出:
C:\Windows\system32>netsh wlan show profiles
Profiles on interface Wi-Fi:
Group policy profiles (read only)
---------------------------------
<none>
User profiles
-------------
All User Profile : WLan foo bar
All User Profile : Router
All User Profile : dLink Wireless
Current User Profile : optimumwifi
token= 1 2 3 * (rest)
forVar %%F \___%%G______/
C:\Windows\system32>
考虑到
- 前导分隔符将被忽略,
- 相邻的只算一个,
使用
:: Q:\Test\2019\03\03\SU_1410873.cmd
@Echo off
For /F "tokens=3* delims=: " %%F IN ('
netsh wlan show profiles ^| Findstr /C:" " ^|Findstr ":"
') DO echo netsh wlan show profile name="%%G" key=clear
要获取这些(仅用于演示回显)命令:
> SU_1410873.cmd
netsh wlan show profile name="WLan foo bar" key=clear
netsh wlan show profile name="Router" key=clear
netsh wlan show profile name="dLink Wireless" key=clear
netsh wlan show profile name="optimumwifi" key=clear