如何检查字符串是否在Windows .bat文件中包含子字符串

时间:2019-10-19 11:41:42

标签: android batch-file

我正在尝试创建.bat脚本,以通过adb(Android调试桥)在android设备上安装.apk文件。到目前为止,这是我的源代码:

@echo off
@echo Plug in ONE VR headset and enable USB debugging on the device
@echo Press any button to continue
PAUSE

:step0
CD /d C:\Users\alexq\AppData\Local\Android\Sdk\platform-tools
adb kill-server
adb start-server
:step1
FOR /F "tokens=*" %%g IN ('adb devices') DO SET result=%%g
echo %result% | FINDSTR /I "device">nul && IF ERRORLEVEL 1 GOTO step2 ELSE GOTO step3

:step2
echo Could not find attached device or USB debugging is not enabled
echo Check USB connection of VR headset
echo Enable USB debugging on the VR headset
echo Press any button to try again
PAUSE
GOTO step1

:step3
echo found device
rem WIP more stuff will be added
PAUSE

我的问题似乎在echo %result% | FINDSTR /I "device">nul && IF ERRORLEVEL 1 GOTO step2 ELSE GOTO step3行中。 echo %result%的输出是一个设备编号,当设备准备好时,它是单词 device ,这里是一个示例:61047ce9 device(是的,在数字和字设备)。

我希望脚本检查此结果中是否包含单词device,然后在找到该单词时转到步骤3。 我的问题是,现在脚本始终跳至step2,因此设备甚至还没有准备好(我手动对其进行了检查)。 有人知道我的错误在哪里吗?

提前

1 个答案:

答案 0 :(得分:0)

以下是根据我的评论提出的答案:

@Echo Off
CD /D "%LocalAppData%\Android\Sdk\platform-tools" 2>NUL||Exit /B 1
Echo Plug in ONE VR headset and enable USB debugging on the device
Pause

ADB kill-server
ADB start-server

:Step1
Set "result="
For /F %%A In ('ADB Devices 2^>NUL^|Find "device"') Do Set "result=%%A"
If Defined result GoTo Step3

:Step2
Echo Could not find attached device or USB debugging is not enabled
Echo Check USB connection of VR headset
Echo Enable USB debugging on the VR headset
Pause
GoTo Step1

:Step3
Echo found device %result%
Rem WIP more stuff will be added
Pause