ADB 上传文件并使用脚本监控上传百分比
有个需求,需要测试 emmc的外部连续写入性能,使用 ADB 上传一个巨大的文件。并且在上传到一定值时进行干预。
因此但是 adb push 命令本身会 block 运行并且不返回进度,因此需要一个额外的监控脚本。
上传脚本:
@echo off
setlocal enabledelayedexpansion
set COUNTER=0
set TARGET_DEVICE=xxx
set ISO_FILE=C:\Users\xxx\Desktop\1.iso
set DEST_PATH=/sdcard/1
:READ
echo Pushing ISO file on round ...
adb -s %TARGET_DEVICE% push %ISO_FILE% %DEST_PATH%
if errorlevel 1 (
echo Push failed, retrying...
goto READ
)
echo File pushed successfully.
adb -s %TARGET_DEVICE% shell rm %DEST_PATH%
if errorlevel 1 (
echo Remove failed, retrying...
goto READ
)
echo File removed successfully.
set /a COUNTER+=1
echo Number of successful operations: !COUNTER!
:: 等待 5 秒
timeout /t 5 > nul
exit
监控脚本
@echo off
setlocal enabledelayedexpansion
:: 这里设置目标设备、目标路径和源文件路径
set TARGET_DEVICE=xxx
set DEST_PATH=/sdcard/1
set ISO_FILE=C:\Users\xxx\Desktop\1.iso
:: Get the total size of the ISO file
for %%F in ("%ISO_FILE%") do set TOTAL_SIZE=%%~zF
:MONITOR_PROGRESS
:: Check the size of the file on the device
::echo 读取文件大小
adb -s %TARGET_DEVICE% shell ls -l %DEST_PATH% > tempfile.txt
if errorlevel 1 (
echo Failed to get file size.
exit /b
)
:: 从 tempfile.txt 中提取文件大小
for /f "tokens=4" %%S in (tempfile.txt) do (
set TRANSFERRED_SIZE=%%S
)
::set /a 命令仅支持 32 位整数,这意味着它不能处理超过 2147483647 的数值
:: 批处理默认的set /a 变量=算式 的结果要在[-2147483647,2147483647]这个范围,超过了会给异常提示
echo.
:: 计算并显示进度百分比
echo TRANSFERRED_SIZE=!TRANSFERRED_SIZE! TOTAL_SIZE=!TOTAL_SIZE!
:: 如果无法读取转移大小,则输出错误信息
if "!TRANSFERRED_SIZE!"=="" (
echo Could not read the transferred file size. It may not exist yet.
) else (
:: 使用 PowerShell 进行比较和计算
for /f %%i in ('powershell -command "[double]!TRANSFERRED_SIZE! -lt [double]!TOTAL_SIZE!;"') do set "result=%%i"
echo result=!result!
if "!result!"=="True" (
for /f "delims=" %%p in ('powershell -command "[math]::Round((%TRANSFERRED_SIZE% -as [double]) / (%TOTAL_SIZE% -as [double]) * 100, 2)"') do set PERCENTAGED=%%p
echo 传输百分比: !PERCENTAGED!
:: 睡眠一秒
timeout /t 1 > nul
:: 开启下次循环
goto MONITOR_PROGRESS
) else (
echo Progress: 100%
)
)
:: Clean up
del tempfile.txt
echo Transfer completed successfully.
pause