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
Проверено с использованием следующего сценария с жестко заданным гипотетическим выводом adb
(без установки adb
):
@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
Вывод : D:\bat\SO\61588773.bat
transport_id 12 (0123456789ABCDEF)
transport_id 11 (F9NPFP084096)
transport_id 10 (abc123def567)
transport_id (noTransportId)