0

This answer is what I want to do, but it works for strings NOT command output.

My script is:

@echo off 
setlocal enabledelayedexpansion

for /F "skip=1 tokens=1,3,5,7" %%a in ('adb devices') do (
echo %%a

set dev1=%%a
set dev2=%%b
set dev3=%%c
set dev4=%%d
)
echo.
echo Devices are: !dev1!, !dev2!, !dev3!, !dev4!
pause

But it shows 1 device, instead of the 2 available:

770a56
emulator-5554

Devices are: emulator-5554, , ,
Press any key to continue . . .

Sample output from Windows:

C:\Windows\Right\Console2>adb devices
List of devices attached
BH90W3T416      device
HSMPG9265D606183        device
emulator-5554   device
2
  • 1
    Can you show sample output of adb devices? Commented Apr 16, 2017 at 1:53
  • LotPings added one, Thank you all. Commented Apr 16, 2017 at 15:46

2 Answers 2

1

In order to store several values, each one in its own variable when such a number is not known in advance (why do you use 4 variables if there are 2 devices?), you need to use an array:

@echo off 
setlocal enabledelayedexpansion

set n=0
for /F "skip=1" %%a in ('adb devices') do (
   echo %%a

   set /A n+=1
   set dev!n!=%%a
)
echo/
echo Devices are: 
for /L %%i in (1,1,%n%) do echo !dev%%i!
pause
Sign up to request clarification or add additional context in comments.

1 Comment

Know any book (not site) that help in doing batch files?
0
...
set dev1=!dev1!,%%a
set dev2=!dev2!,%%b
set dev3=!dev3!,%%c
set dev4=!dev4!,%%d
...
echo Devices are: !dev1:~1!, !dev2:~1!, !dev3:~1!, !dev4:~1!

which will establish a list for each dev with a leading comma.

Since you don't show us raw input and don't explain why you are specifying 4 tokens but showing only one, your question is unclear. If %%a is the only token of interest, then

...
set "dev4=!dev3!"
set "dev3=!dev2!"
set "dev2=!dev1!"
set "dev1=%%a"
...

may be more suitable.

1 Comment

I edited the raw command output in the question to clarify. Taking a tip from your 1st code, I can just do: set devices=!devices!,%%a Then split the devices string by commas and parse them like in here, thanks it works. Strangely the 2nd part of your code truncates the 1st letter of each device, but that's not a problem now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.