2016-06-16 31 views
2

我想在我的批處理腳本(bat)中獲得某個comport編號, 然後我可以對此comport進行進一步操作。windows批處理文件編程 - 獲取COMPORT編號

例如,我想下面的USB診斷端口號: 高通公司HS-USB診斷9091(COM168)

如何獲得在批處理文件中的端口號(168)? 謝謝。

+0

...你通常如何得到它? – SomethingDark

+0

我必須從用戶那裏獲得輸入信息,以知道哪個端口可以訪問,哪個端口不夠自動。 – tcy925

+0

回答自己 – tcy925

回答

0
@echo off 
setlocal 

:: wmic /format:list strips trailing spaces (at least for path win32_pnpentity) 
for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| find "COM"') do (
    call :setCOM "%%~J" 
) 

:: display all _COM* variables 
set _COM 

:: end main batch 
goto :EOF 

:setCOM <WMIC_output_line> 
:: sets _COM#=line 
setlocal 
set "str=%~1" 
set "num=%str:*(COM=%" 
set "num=%num:)=%" 
echo %num% 
set str=%str:(COM=&rem.% 
echo %str% 

if %str% == "Qualcomm HS-USB Diagnostics 9091" 
echo port number is %num% 

endlocal & set "_COM%num%=%str%" 

goto :EOF