循环直到变量!=“”

时间:2014-05-16 16:26:13

标签: batch-file while-loop adb dos

我想创建一个批处理文件,将某些值发送到output.txt。我有以下代码:

for /F "delims=" %%a in ('adb logcat -v threadtime | grep -m 1 'VAC: App Name is:'') do set appName=%%a

echo App Name= %appName% > output.txt

问题是grep语句没有立即返回值 - 直到用户在Android设备上执行某个操作。所以我需要围绕" for"创建一个循环。声明。我会在下面尝试,但我知道这是错误的。

do
while 
%appName% = ""
    for /F "delims=" %%a in ('adb logcat -v threadtime | grep -m 1 'VAC: App Name is:'') do set appName=%%a

    if "%appName% != "" leave
enddo

echo App Name= %appName% > output.txt

2 个答案:

答案 0 :(得分:0)

set "appName="
:loop
for /F "delims=" %%a in (
    'adb logcat -v threadtime ^| grep -m 1 "VAC: App Name is:"'
) do set "appName=%%a"
if not defined appName goto loop

它只是清除变量并保持循环,直到它获得任何值

答案 1 :(得分:0)

在这里(虽然MC ND的答案看起来也不错): 请注意管道需要由插入符号^转义,我添加了1秒等待ping -n 1 -w 1000 1.1.1.1 >nul

:while_loop
for /F "usebackq delims=" %%a in (`adb logcat -v threadtime ^| grep -m 1 'VAC: App Name is:'`) do set appName=%%a
ping -n 1 -w 1000 1.1.1.1 >nul
if not "%appName% == "" goto exit_loop
goto while_loop


:exit_loop
echo App Name= %appName% > output.txt