如何同时在多个adb设备上使用批处理文件?

时间:2015-05-07 14:39:01

标签: android batch-file cmd adb

我正在尝试通过cmd同时在多个adb设备上使用批处理文件。

如何设置名为 serial 的变量并使用adb -s对所有连接的设备运行命令?

2 个答案:

答案 0 :(得分:0)

这就是我为了在多个不确定的设备上使用屏幕截图而做的事情;

@echo off
setlocal enableDelayedExpansion
REM Some Variables To Create Directories with
set VERSION=APPVERSION
set TEST=TESTNAME
REM used to remove 'device' from each line
set "replace=device"
set "replaced="
set "source=test.txt"
set "target=clean.txt"
REM adb command to grab all connected devices
adb devices > adb.txt
REM Used to generate a filename friendly datetime
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set datetime=%datetime:~0,8%-%datetime:~8,6%
REM Strip everything except the device ID/Serial Number
findstr /v "List of devices attached" adb.txt > test.txt
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!)
) > %target%
REM with the 'clean' file of just device IDs, set them as elements of an array
set idi=0
for /F "usebackq" %%A in (clean.txt) do (
    SET /A idi=!idi! + 1
    set var!idi!=%%A
)
set var
REM now the true work can begin
REM we use the device ids to take and save a screenshot from each device
REM we are putting the screenshots into a device specific folder in VERSION\TEST directory
REM the screenshot name is formatted as 'deviceID-datetime.ping'
for /L %%x in (1, 1, %idi%) do (
if not exist "%UserProfile%\Desktop\%VERSION%\%TEST%\!var%%x!" mkdir %UserProfile%\Desktop\%VERSION%\%TEST%\!var%%x!
adb -s !var%%x! shell screencap -p /sdcard/!var%%x!-%datetime%.png
adb -s !var%%x! pull /sdcard/!var%%x!-%datetime%.png %UserProfile%\Desktop\%VERSION%\%TEST%\!var%%x!
adb -s !var%%x! shell rm /sdcard/!var%%x!-%datetime%.png
)

您可能最关心的部分是识别您希望在每台设备上执行的命令中使用的所有连接设备序列号的部分;

REM used to remove 'device' from each line
set "replace=device"
set "replaced="
set "source=test.txt"
set "target=clean.txt"
REM adb command to grab all connected devices
adb devices > adb.txt
findstr /v "List of devices attached" adb.txt > test.txt
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!)
) > %target%
REM with the 'clean' file of just device IDs, set them as elements of an array
set idi=0
for /F "usebackq" %%A in (clean.txt) do (
    SET /A idi=!idi! + 1
    set var!idi!=%%A
)
set var

从那里你可以修改最后一位以包含你的命令;

for /L %%x in (1, 1, %idi%) do (
adb -s !var%%x! shell <command>)

可能有一种“更清洁”的方式,但这符合我的目的。

答案 1 :(得分:0)

有多种方法可以做到这一点

方式#1:

批处理文件名:testbat.bat

SET adbserial=%1
adb -s %adbserial% reboot
adb -s %adbserial% wait-for-device
adb -s %adbserial% root

testbat.bat 123456运行,其中123456是您的adb序列号

方式#2:

  1. 打开命令提示符
  2. SET ANDROID_SERIAL 123456,其中123456是您设备的adb序列号之一
  3. 运行批处理脚本而不做任何修改。
  4. 该特定命令提示符只能与序列号为“123456”

    的设备通信