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



我正在尝试创建一个.bat脚本,以通过 adb(安卓调试桥(在安卓设备上安装.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:UsersalexqAppDataLocalAndroidSdkplatform-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%的输出是设备编号和设备准备就绪时的单词设备,这里有一个例子:61047ce9 device(是的,数字和单词设备之间有这些空格(。

我希望脚本检查单词设备是否在此结果中,然后在找到它时转到步骤3。 我的问题是现在脚本总是跳到 step2,所以设备还没有准备好,甚至认为它是(我手动检查了它(。 有人知道我的错误在哪里吗?

提前感谢

以下是基于我评论建议的答案:

@Echo Off
CD /D "%LocalAppData%AndroidSdkplatform-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

最新更新