我想在cmd中顯示你連接的網路名稱,我嘗試創建一個腳本
netsh wlan show interfaces | findstr /C:"SSID:"
但是當我運行腳本時,它顯示了這個結果
SSID: networkname
我想要一種只顯示網路名稱而不顯示空格且不顯示單詞SSID和:
uj5u.com熱心網友回復:
我會提供使用 WMI 的替代方案:
獲得幫助wmic:
@For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Where
"IPv4Connectivity='4' And Name Is Not Null" Get Name /Format:MOF 2^>NUL'
) Do @Echo %%G
獲得幫助腳本:
<!-- ::Script Language="Batch"
@For /F %%G In ('%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"')Do @Echo(%%G
@Pause&Exit /B
--><Job><Script Language="VBScript">
Set objWMI = GetObject("WINMGMTS:\\.\ROOT\StandardCimv2")
WQL = "Select * From MSFT_NetConnectionProfile" _
" Where IPv4Connectivity='4' And Name Is Not Null"
Set Instances = objWMI.ExecQuery(WQL)
For Each Instance In Instances
Wscript.Echo Instance.Name
Next</Script></Job>
如果您想要SSID使用初始命令,您應該知道 NetSh 的輸出是表格格式,并且可以包含或排除空格字符。因此,我可能會提供這種型別的方法:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SSID=" & For /F "Tokens=1,* Delims=:" %%G In ('
%SystemRoot%\System32\netsh.exe WLAN Show Interfaces 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /R /C:"^[ ][ ]*SSID[ ][ ]*:[ ][ ]*[^ ][^ ]*"'
) Do (Set "SSID=%%H" & SetLocal EnableDelayedExpansion
For /F Delims^=^ EOL^= %%I In ("!SSID:~1!") Do EndLocal & Set "SSID=%%I")
If Defined SSID (Echo "%SSID%"
Pause)
EndLocal
GoTo :EOF
使用相同的方法,您也可以隔離Profile名稱:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Profile=" & For /F "Tokens=1,* Delims=:" %%G In ('
%SystemRoot%\System32\netsh.exe WLAN Show Interfaces 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /R
/C:"^[ ][ ]*Profile[ ][ ]*:[ ][ ]*[^ ][^ ]*"') Do (Set "Profile=%%H"
SetLocal EnableDelayedExpansion & For /F Delims^=^ EOL^= %%I In (
"!Profile:~1!") Do EndLocal & Set "Profile=%%I")
If Defined Profile (Echo "%Profile%"
Pause)
EndLocal
GoTo :EOF
您可能會注意到,正如我經常遇到的那樣,您的結果包含一個尾隨空格,這可能會證明是有問題的。因此,我建議您不要依賴 NetSh 的表格輸出。
uj5u.com熱心網友回復:
使用for /f:
for /f "usebackq tokens=2 delims= " %%N in (`netsh wlan show interfaces ^| findstr /C:"SSID:") do (
set nw_name=%%N
echo Found a network: !nw_name!
)
這將在空格 () 上for拆分過濾netsh命令 ( ) 的結果,將采用第二個標記 ( ),然后您將在回圈變數中找到它。注意把它放在另一個變數中,因為一旦回圈完成就不再存在了。usebackqdelimstokens%%N%%N
有關詳細資訊,另請參見for /?命令提示符。
uj5u.com熱心網友回復:
我很高興看到你已經有了答案。這是另一種方法,它將在運行中batch-file運行cmd。
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"(Get-NetConnectionProfile | Where-Object InterfaceAlias -eq 'Wi-Fi').Name"') DO (SET "nw_name=%%~A")
ECHO %nw_name%
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/455193.html
