批处理循环之安卓adb批量遍历枚举

这是批处理的基本语法的典型, 设置变量,循环,注释等等

需要批量处理连接电脑的每一个安卓设备执行adb命令

连接模式1:USB调试,数据线

@echo off<nul 3>nul
REM "注释(不显)无黑窗口"
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
:begin
rem 设置延迟变量
setlocal EnableDelayedExpansion

REM "注释(不显)支持UTF-8文档"
chcp 65001>null
REM "注释(不显)延时"
ping 127.0.0.1 -n "2">null
timeout 3
REM "注释(不显)跳转coutinue标记"
:coutinue
adb wait-for-devices
echo "设备已连接."
adb devices |findstr /v List>devices.txt
FOR /F "tokens=1 delims=	" %%i in (devices.txt) DO (
set DeviceName=%%i
echo "设备名称: !DeviceName!."
REM "注释(不显)执行命令:adb -s %%i install app.apk"
scrcpy.exe -s %%i 
)
:: "注释(不显)跳转:goto coutinue"

其中 setlocal EnableDelayedExpansion 才能set定义变量后 在 echo 中使用.

delims=空白跟的是一个tab键空白符

连接模式2: WIFI调试

@echo off
rem 设置延迟变量
setlocal enabledelayedexpansion
rem 删除输出文件
SET ConnectedFile=AdbConnected.txt
SET AllDeviceFile=AdbAllDevices.txt
if exist %ConnectedFile% (
    del /f /s /q %ConnectedFile%
)
if exist %AllDeviceFile% (
    del /f /s /q %AllDeviceFile%
)
rem 定义数组,从index到loopCnt,可以自定义
set loopCnt=254
set index=1
for /l %%a in (%index%,1,%loopCnt%) do (set array[%%a]=%%a)
rem 开始循环执行指令
:BeginLoop
if %index% GTR %loopCnt%  goto EndLoop
set idx=!array[%index%]!
rem 执行指令
set "cmd=adb connect 192.168.31.%idx%:5555"
echo %cmd%
rem 把执行结果存储到变量中
for /f "delims=" %%j in ('%cmd%') do set ret=%%j
rem echo %ret%
echo %ret% >> %AllDeviceFile%
rem 这里可以执行adb命令...
echo %ret%| findstr "connected" >nul &&(
    echo "开始执行adb"
    rem adb -s 192.168.31.%idx%:5555 pull /sdcard/01.jpg d:/log
    scrcpy.exe -s 192.168.31.%idx%:5555
)
adb disconnect 192.168.31.%idx%:5555
set /a index=%index%+1
goto BeginLoop
:EndLoop
rem 看看哪些设备连接成功了
findstr /i "connected" %AllDeviceFile% > %ConnectedFile%
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注