Windows – How to split variable by colon in Batch

batchbatch filecommand linewindows

I am using batch for extracting information from adb devices -l, this command shows messages like this:

123456789012345    device product:abc model:ABC device:abc transport_id:7

I would like to get transport_id 7 from this string transport_id:7, so I try to split messages by space at first, and it works fine. But while trying to split by colon, I got an error said The system cannot find the file transport_id:7. What am I doing wrong ?

Here is my code.

@ECHO off
for /f "tokens=1,2,3,4,5,6" %%a in ('adb.exe devices -l') do (
    if "%%b" == "device" ( 
        ECHO Serial Number : %%a
        ECHO Transport Id  : %%f
        for /f "tokens=2 delims=:" %%A in ( %%f ) Do @Echo %%A
        call test.bat %%A
    )
)
PAUSE

Output and error:

Serial Number : 123456789012345
Transport Id  : transport_id:7
The system cannot find the file transport_id:7.

What does this error message The system cannot find the file transport_id:7 means ?

Best Answer

The correct formulation for solving the problem is:

for /f "tokens=2 delims=:" %%A in ("%%f") Do @Echo %%A

Without the double-quotes around the %%f, it is taken as a file-name. The double-quotes cause it to be treated as a string.

Related Question