0

I am trying to parse the out put of MODE command in command prompt and assign it to a variable. The out put of mode is as shown below,

PS C:\Users\test> mode %COMPORT%

Status for device COM5:
-----------------------
    Baud:            9600
    Parity:          None
    Data Bits:       8
    Stop Bits:       1
    Timeout:         OFF
    XON/XOFF:        OFF
    CTS handshaking: OFF
    DSR handshaking: OFF
    DSR sensitivity: OFF
    DTR circuit:     OFF
    RTS circuit:     OFF

I'm trying to get the first line using FIND as shown below,

mode COM5 | find /I "Baud"

it says FIND: Parameter format not correct

btw, this is how the whole code looks like,

@echo off
for /f "tokens=3" %%a in (
    'REG QUERY HKLM\HARDWARE\DEVICEMAP\SERIALCOMM'
) do set "COMPORT=%%a" 
echo %COMPORT%

for /f "tokens=2" %%a in (
    'MODE %COMPORT% | FIND /I "Baud"'
) do set "SPEED=%%a" 
echo %SPEED%

But this is not working, what am i doing wrong?

Jimson James
  • 2,937
  • 6
  • 43
  • 78
  • but you want this as powershell code or as cmd code? not quite clear. In CMD what you're looking for is the `findstr /i` command https://ss64.com/nt/findstr.html – Santiago Squarzon Mar 12 '21 at 21:52
  • It looks like the powershell console is treating this command diferently compared to the CMD. In normal command prompt this command runs fine, not in powershell console. btw, there is no need to use powershell console, but I got curious why it is failing! – Jimson James Mar 12 '21 at 22:00
  • 1
    This is the PowerShell equivalent: `'MODE %COMPORT% | Select-String "Baud"'` or `'MODE %COMPORT% | sls "Baud"'` cause I love alias :) – Santiago Squarzon Mar 12 '21 at 22:04
  • 1
    Or, without piping: `for /f "tokens=1*" %%a in ('MODE %COMPORT%') do if /i "%%a"=="baud:" set "speed=%%b"`. – dxiv Mar 12 '21 at 23:49

1 Answers1

0

Eventhough its still unknown to me why the same command works in a normal console but not in a powershell console, the problem with the script was entirely at a different place. Inside the for loop you need to escape the | character. It was not the FIND command's Parameter format not correct error the real problem, (a false alarm), but the escaping needed for the |

Corrected script is as shown below.

@echo off
for /f "tokens=3" %%a in (
    'REG QUERY HKLM\HARDWARE\DEVICEMAP\SERIALCOMM'
) do set "COMPORT=%%a" 
echo %COMPORT%

for /f "tokens=2" %%a in (
    'MODE %COMPORT% ^| FIND /I "Baud"'
) do set "SPEED=%%a" 
echo %SPEED%
Jimson James
  • 2,937
  • 6
  • 43
  • 78