admin管理员组

文章数量:1277380

I have an old batch file that start an exe passing some hardcoded parameters, and I need to make it more flexible.

One of those parameters is the serial port number to use, and what I want now is to prompt the used to pick the right one.

I've found this great answer to find and show the ports descriptions. On my PC, it shows this list:

_COM1=Porta di comunicazione
_COM2=Porta di comunicazione
_COM5=USB Serial Port
_COMRealtek RealManage COM1=Realtek RealManage COM1
_COMRealtek RealManage COM2=Realtek RealManage COM2

What i would like is:

  • Assign the right number to each port (in my case: 1,2 and 5)
  • Allow the user to choose one of them and store the number in a variable (checking if the answer is correct, or ask again)
  • Optionally, get rid of the last, duplicate ports listed

So, in my case, i would like to show to the user something like this:

Please select what COM port to use:
  1 - COM1=Porta di comunicazione
  2 - COM2=Porta di comunicazione
  5 - COM5=USB Serial Port
or press ESC to exit.

How could it be done?

I have an old batch file that start an exe passing some hardcoded parameters, and I need to make it more flexible.

One of those parameters is the serial port number to use, and what I want now is to prompt the used to pick the right one.

I've found this great answer to find and show the ports descriptions. On my PC, it shows this list:

_COM1=Porta di comunicazione
_COM2=Porta di comunicazione
_COM5=USB Serial Port
_COMRealtek RealManage COM1=Realtek RealManage COM1
_COMRealtek RealManage COM2=Realtek RealManage COM2

What i would like is:

  • Assign the right number to each port (in my case: 1,2 and 5)
  • Allow the user to choose one of them and store the number in a variable (checking if the answer is correct, or ask again)
  • Optionally, get rid of the last, duplicate ports listed

So, in my case, i would like to show to the user something like this:

Please select what COM port to use:
  1 - COM1=Porta di comunicazione
  2 - COM2=Porta di comunicazione
  5 - COM5=USB Serial Port
or press ESC to exit.

How could it be done?

Share Improve this question asked Feb 25 at 12:10 ParduzParduz 6025 silver badges26 bronze badges 18
  • You are not new to this platform, so there is no excuse not to have used the search facility at the top of the page. What you are expected to do is locate code which performs similar tasks, then write your own based upon those results. We aren't here to create your code for you, based upon your set of requirements, nor are we here to tutor you through the process. – Compo Commented Feb 25 at 13:03
  • @Compo — I did not get it. What search facility? The inquirer did not ask to develop code, the request is about some batch techniques which are good to know. The inquirer provided an initial piece of code, clearly written and illustrative. What's wrong? – Sergey A Kryukov Commented Feb 25 at 13:42
  • The code linked gave me different results from that posted. This forces the dangerous approach of making assumptions. – Magoo Commented Feb 25 at 14:36
  • @SergeyAKryukov : You mean you've been here for four months and haven't noticed yje search facility with the edit box and the magnifying glass on the very top line ? :D – Magoo Commented Feb 25 at 14:40
  • @Magoo — Thank you for the clarification. Yes, I know it, but I don't understand why you blame the inquirer. I also don't understand your words about dangerous approach. Perhaps the problem is that you always express yourself not always clearly. For example, there is no code linked, but there is the page referenced. – Sergey A Kryukov Commented Feb 25 at 14:48
 |  Show 13 more comments

2 Answers 2

Reset to default 0
@ECHO OFF
SETLOCAL
cls
SET /a menu=0
SET "choices="

for /f "tokens=1* delims==" %%I in ('wmic path win32_pnpentity get caption /format:list ^| findstr /e /R "(COM.)"') do SET "name="&CALL :process %%J

choice /c q%choices% /M "Choose a number or Q to quit"
SET /a choicemade=%errorlevel%-1
IF %choicemade%==0 GOTO :eof
for /f "tokens=1* delims==" %%I in ('SET menuline') DO IF /i "%%I"=="menuline%choicemade%" ECHO choice made was (%choicemade%) %%J
GOTO :EOF
:process
SET "name=%name% %~1"
IF "%~2" neq "" shift&GOTO process
SET "comport=%~1"
SET "comport=%comport:~1,-1%"
SET /a menu+=1
SET "choices=%choices%%menu%"
ECHO %menu%: %comport%%name%
SET menuline%menu%=%comport%%name%

GOTO :eof

No code, so no explains.

I would suggest the following solution:

@echo off  

:request-port
echo Please select what COM port to use:
echo   1: COM1=Porta di comunicazione
echo   2: COM2=Porta di comunicazione
echo   5: COM5=USB Serial Port
echo or enter "exit" to exit
set /p port=Port (1, 2, or 5) 
if "%port%"=="1" ( goto :continue )
if "%port%"=="2" ( goto :continue )
if "%port%"=="5" ( goto :continue )
if "%port%"=="exit" ( goto :eof )
goto :request-port
:continue

echo Selected port: %port% & :: Start your application here instead

In place of the last line, you can start your application and pass %port% to its command line.
Note that & :: starts an end-of-line comment.

I tested it, everything works correctly.

Unfortunately, I don't think there is a simple way to detect Escape. Instead, I suggested entering exit. Well, the features of the batch language are badly limited.

However, there is a more flexible solution based on subroutines and call :label, a newer feature of the batch language. Note that goto :eof works like return.

@echo off

call :request-port
if "%port%"=="exit" ( goto :eof )
echo Selected port: %port% & :: Start your application here instead
goto :eof

:request-port
echo Please select what COM port to use:
echo 1: COM1=Porta di comunicazione
echo 2: COM2=Porta di comunicazione
echo 5: COM5=USB Serial Port
echo or enter "exit" to exit
set /p port=Port (1, 2, or 5): 
if "%port%"=="1" ( goto :eof )
if "%port%"=="2" ( goto :eof )
if "%port%"=="5" ( goto :eof )
if "%port%"=="exit" ( goto :eof )
goto :request-port

To overcome the limitations of the batch language, you may think of using some command-line scripting alternatives, such as PowerShell, node.js, Python, and the like. Unfortunately, only .cmd files work out-of-box, everything else requires installation and setting up your environment.

本文标签: Windows Batch to select a COM port numberStack Overflow