2

I am struggling to get the output required.

I am using a 2 commands to get 2 variables on a device, across multiple devices. I am unsure how to cross reference these to get the output I would like. I am trying to see if any of the contents of DEVPACKAGE appears in the contents of packages, if they don't appear in packages return an error. I assume it is something similar to [Windows Batch: How to set the output of one command as a variable and use it in another command? but I cannot see how to apply it to my current variables.

I understand that some of my methods may be crude. I have only been seriously looking into batch file construction for a week.

DEVPACKAGE, Package and attempted output are as below:

::Global
@echo off

set AAPT=tools\aapt.exe
set GREP=tools\grep.exe
set CUT=tools\cut.exe

:: Check Gold Build applications
cls
@echo.
@echo ------------------------ CHECK APPLICATIONS INSTALLED --------------------------

SETLOCAL ENABLEDELAYEDEXPANSION
::EXTRACT PACKAGENAME FROM APK
FOR /F "tokens=1,2 skip=1" %%N IN ('adb devices') DO (
    SET IS_DEV=%%O
    if "!IS_DEV!" == "device" (
        SET SERIAL=%%N
        for /f "delims=" %%P in ('dir /b ^"APKs\*.apk^"') do (
            SET APK=%%P
            for /f "tokens=1 delims=" %%Q in ('%AAPT% d badging APKs\!APK! ^| !GREP! "package: name=" ^| !CUT! -d' -f2') do (
                set package=%%Q
                if "!package!" == "" set package=Unknown (
                )
            )
        )
    )
)
::EXTRACT INSTALLED PACKAGENAME
FOR /F "tokens=1,2 skip=1" %%R IN ('adb devices') DO (
    if "!IS_DEV!" == "device" (
        FOR /F "tokens=1 delims=" %%U IN ('adb shell "pm list packages" ^| !CUT! -f 2 -d ":"^') DO (
        SET DEVPACKAGE=%%U
                )
            )
        )
    )
)
::CHECK IF INSTALLED APPEARS IN PACKAGENAME FROM APK FOLDER
FOR /F "tokens=1,2 skip=1" %%V IN ('adb devices') DO (
    if "!IS_DEV!" == "device" (
        for /f "delims=" %%W in ('dir /b ^"APKs\*.apk^"') do (
            IF !DEVPACKAGE! NEQ !package! (
                echo Device !SERIAL! does not have !package! installed
                ) else (
                echo Device !SERIAL! has all APKs installed correctly
                    )
                )
            )
        )
    )
)
ENDLOCAL
@pause

I would a variable output similar to:

------------------------ CHECK APPLICATIONS INSTALLED --------------------------

Device <SERIAL1> does not have <APK2> installed
Device <SERIAL3> does not have <APK1> installed
Device <SERIAL22> does not have <APK7> installed

Press any key to continue . . .

Any help would be greatly appreciated. Thanks in advance.

Community
  • 1
  • 1
beecho01
  • 85
  • 9

1 Answers1

1

The easier way:

setlocal enableDelayedExpansion
set "DEVPACKAGE_=!DEVPACKAGE:"=""!"
echo !package!|find "!DEVPACKAGE_!" >nul 2>nul && (
   echo it is contained
)||(
   echo it is NOT contained
)

The temporary variable DEVPACKAGE_ is needed to double the " so if the string contains it will be processed correctly by the FIND command.

You can do it with cmd internals commands only too:

setlocal enableDelayedExpasion
if "%package%" equ "!package:%DEVPACKAGE%=!" (
  echo it is NOT contained
) else (
  echo it is contained
)

In theory the second approach should be faster but there more characters that could break it.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • I have added this to the end and it is just bringing through `it is NOT contained`. Have i done something wrong here as there is not accosiated `!package!` name? Sorry if its a silly question – beecho01 Feb 22 '17 at 10:49
  • @beecho01 - you need to use [variable replacement](https://ss64.com/nt/syntax-replace.html) but not directly to compare the devpackage and package. If all occurrences of `devpackage` are replaced in `package` and the strings are equal then it is not contained. At the moment you are just checking if `devpackage` is equal to `package` - try with the whole condition copied. – npocmaka Feb 22 '17 at 11:12