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
Community
  • 1
  • 1

2 Answers2

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
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 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](http://stackoverflow.com/a/8495889/7748586), thanks it works. Strangely the 2nd part of your code truncates the 1st letter of each device, but that's not a problem now. –  Apr 16 '17 at 19:46