-1

I have and batch file script shows me output of adb utility.

FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO echo %%x

2 output lines

0123456789ABCDEF       device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12
F9NPFP084096           device product:WW_P023 model:P023 device:P023_1 transport_id:11

I want parse non-space substring after "transport_id:". For the first line i want get 12, for second 11. How can I do that?

Amir Kadyrov
  • 1,253
  • 4
  • 9
  • in general, you want to get the last token when the string is delimited by `:`. if this is the case (correct me if it's not), this has already been covered in https://stackoverflow.com/questions/5473840/last-token-in-batch-variable – ScriptKidd May 04 '20 at 09:57
  • Not last token. If adb will change output format, it may be last, first, second or penult token. – Amir Kadyrov May 04 '20 at 10:14
  • 1
    What have you tried, where are you stuck? You could, for example, replace the sub-string by a character that cannot occur anywhere else, then you have gained a delimiter for `for /F` to work with... – aschipfl May 04 '20 at 11:56

3 Answers3

2

If I understand correctly what you've asked, the following should output the information you require.

@Echo Off

For /F "Skip=1 Tokens=1*" %%G In ('adb.exe devices -l') Do (Set "DI=%%H"
    SetLocal EnableDelayedExpansion
    For /F %%I In ("!DI:*transport_id:=!") Do EndLocal & Echo %%I)

Pause

And, if you wanted to do that from :

For /F "Skip=1Tokens=1*" %G In ('adb.exe devices -l')Do @Set "DI=%H"&For /F %I In ('Cmd /D/V/C Echo "!DI:*transport_id:=!"')Do @Echo %~I
Compo
  • 36,585
  • 5
  • 27
  • 39
  • This method will fail if the desired token is _"Not last token. If adb will change output format, it may be last, first, second or penult token"_. In this case the returned value include the string after it... – Aacini May 04 '20 at 14:55
  • @Aacini, I'm not sure what you mean. It should work if the line is, for example: `0123456789ABCDEF       device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12`, or `0123456789ABCDEF       device product:java_joyplus_qb7 transport_id:12 model:TM702B4_3G device:java_joyplus_qb7`, or `0123456789ABCDEF       device transport_id:12 product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7`. There are of course ways of grabbing each item, but the question was specific to grabbing only the Transport ID. – Compo May 04 '20 at 16:04
1

You may easily extract all variables no matter their positions and show what you want:

@echo off
setlocal EnableDelayedExpansion

FOR /F "skip=1 delims=" %%a IN ('adb devices -l') DO (
   for %%b in (%%a) do for /F "tokens=1,2 delims=:" %%x in ("%%b") do set "%%x=%%y"

   echo Product=!product!
   echo Transport=!transport_id!
)

For example:

Product=java_joyplus_qb7
Transport=12
Product=WW_P023
Transport=11
Aacini
  • 65,180
  • 12
  • 72
  • 108
1
SETLOCAL EnableDelayedExpansion
REM … 
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO (
    set "_ID="&set "_transport_id="
    call :parse "" "%%~x"
    echo transport_id !_transport_id!  (!_ID!^)
)

REM … remaining part of your script here …

goto :eof

:parse
if "%~2"=="" goto :eof 
for /F "tokens=1,*" %%G in ("%~2") do (
  if "%~1"=="" (
    set "_ID=%%~G"
    call :parse "%%~G" "%%~H" 
  ) else (
    for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
      if /I "%%~g"=="transport_id" (
        set "_transport_id=%%~h"
      ) else (
        call :parse "%%~G" "%%~H" 
      )
    )
  )  
)
goto :eof

Tested using the following script with hard-coded hypothetical adb output (not having adb installed):

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
(
REM last token
set "_ID="&set "_transport_id="
call :parse "" "0123456789ABCDEF       device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12"
echo transport_id !_transport_id!  (!_ID!^)
REM penult token
set "_ID="&set "_transport_id="
call :parse "" "F9NPFP084096           device product:WW_P023 model:P023 transport_id:11 device:P023_1"
echo transport_id !_transport_id!  (!_ID!^)
REM elsewhere token
set "_ID="&set "_transport_id="
call :parse "" "abc123def567           transport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id!  (!_ID!^)
REM nowhere token
set "_ID="&set "_transport_id="
call :parse "" "noTransportId          sport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id!  (!_ID!^)
)
goto :eof

:parse
if "%~2"=="" goto :eof 
for /F "tokens=1,*" %%G in ("%~2") do (
  if "%~1"=="" (
    set "_ID=%%~G"
    call :parse "%%~G" "%%~H" 
  ) else (
    for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
      if /I "%%~g"=="transport_id" (
        set "_transport_id=%%~h"
      ) else (
        call :parse "%%~G" "%%~H" 
      )
    )
  )  
)
goto :eof

Output: D:\bat\SO\61588773.bat

transport_id 12  (0123456789ABCDEF)
transport_id 11  (F9NPFP084096)
transport_id 10  (abc123def567)
transport_id   (noTransportId)
JosefZ
  • 28,460
  • 5
  • 44
  • 83