当前环境:win10 64bit

 

@REM 进入到当前目录

cd /d %~dp0

@ECHO OFF

CLS

@REM 设置命令查找路径

set pythonPath=%cd%\tools\python

set adbPath=%cd%\tools\adb

set aaptPath=%cd%\tools\aapt

set adb_aapt_command=null

@REM 判断path路径是否包含 adb.exe

where adb |find /i "adb.exe" >nul && set hasAdb=true|| set hasAdb=false

@REM 若path路径包含 adb.exe,则设置

if not %hasAdb%==true (

wmic ENVIRONMENT where "name='path' and username=''" set VariableValue="%adbPath%;%path%"

setx path "%path%"

echo "adb.exe has added to path"

)

@REM 判断path路径是否包含 aapt.exe

where aapt |find /i "aapt.exe" >nul && set hasAapt=true|| set hasAapt=false

@REM 若path路径包含 aapt.exe,则设置

if not %hasAapt%==true (

wmic ENVIRONMENT where "name='path' and username=''" set VariableValue="%aaptPath%;%path%"

setx path "%path%"

echo "aapt.exe has added to path"

)

@REM 判断path路径是否包含 %pythonPath%;%adbPath%

@REM echo %path%|find /i "%pythonPath%;%adbPath%" >nul && set IsNull=true|| set IsNull=false

@REM @REM 如果没有设置过,则设置;如果已经设置,则不再设置

@REM if not %IsNull%==true (

@REM wmic ENVIRONMENT where "name='path' and username=''" set VariableValue="%pythonPath%;%adbPath%;%path%"

@REM setx path "%path%"

@REM )

set APK_SAVE_DIR=priv-app

if not exist %APK_SAVE_DIR% mkdir %APK_SAVE_DIR%

@REM 遍历 priv-app 目录下面所有apk文件

set /a sum=0

@REM for %%x in (*.apk) do (

for /r %APK_SAVE_DIR% %%x in (*.apk) do (

echo "----------------------------------------->for below apk:" %%x

python privapp_permissions_for_window.py %%x

echo.

set /a sum=sum+1

)

echo Done %sum% apk.

pause

echo.

 

 

代码片段说明:

@REM 判断path路径是否包含 adb.exewhere adb |find /i "adb.exe" >nul && set hasAdb=true|| set hasAdb=false@REM 若path路径包含 adb.exe,则设置if not %hasAdb%==true (wmic ENVIRONMENT where "name='path' and username=''" set VariableValue="%adbPath%;%path%"setx path "%path%"echo "adb.exe has added to path")

 

1、通过 where adb 命令,查找path是否存在adb命令,若存在,则设置变量   hasAdb=true,若不存在,则设置 hasAdb=false

2、若adb命令不存在path中,通过下面命令将adb命令添加到path中

wmic ENVIRONMENT where "name='path' and username=''" set VariableValue="%adbPath%;%path%"

setx path "%path%"

 

上面这段代码需要通过 管理员身份 运行脚本,然后才能有权限添加到path中

 

 

 

目前有个待解的问题:

上面代码有两个判断语句,依次判断adb、aapt命令,调用了2次下面语句

wmic ENVIRONMENT where "name='path' and username=''" set VariableValue="%adbPath%;%path%"setx path "%path%"

但是,通过 管理员身份 运行脚本时,当时运行1次脚本时,只能让1个命令添加成功,另外一个命令没有添加到path; 若想将命令都添加成功,则需要通过 管理员身份 运行2次脚本,不知道是什么原因,暂时没有解决

 

最终的解决方法,可以看这里,完美解决    见 get_privapp_permissions.bat 文件

查看原文