Search code examples
batch-fileadb

Extract alphanumeric string from line Batch Windows 7


I am working on a batch file that will connect the device selected automatically. This is the beginning of my script:

echo This is the list of connected devices:
adb devices -l | findstr "[0-9]"|find /N " "
set /P device= Select the device you would like to connect to:

This is the output in Windows command prompt:

[1]350800651       device asdsffas
[2]07ea9b921       device iwoequeo

What I am trying now, is to get just the ID number from the device (350800651 or 07ea9b921) that would be the one from the selected row (1 or 2 in the example). Any ideas of how I can do it? I have tried with findstr + regex but it always gives me back the full line...


Solution

  • There is already a linked answer as per the now closed question link.

    There however is another simple workaround... not the best, but might work for you.

    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=2 delims=] " %%i in ('adb devices -l ^| findstr "[0-9]"|find /N " "') do (
       set /p "result=Do you want to select device %%i [Y/N]: "
       if /i "!result!"=="Y" "set device=%%i" & goto next
    )
    :next
    echo do something else here with %device%