windows批处理文件编程 - 获取COMPORT编号

时间:2016-06-16 07:39:57

标签: batch-file

我想在我的批处理脚本(bat)中获得一定的comport编号,   然后我可以对这个程序做进一步的操作。

例如,我想获取以下usb diag端口的数量:   Qualcomm HS-USB诊断9091(COM168)

如何在批处理文件中获取端口号(168)?   感谢。

1 个答案:

答案 0 :(得分: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