Reputation: 602
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:
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?
Upvotes: 0
Views: 106
Reputation: 916
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.
Upvotes: 0
Reputation: 80173
@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.
Upvotes: 0