在管道中使用时查找命令不起作用

时间:2015-04-02 15:44:51

标签: batch-file command-line

echo off

:loop

tasklist /fi "imagename eq Notepad.exe" | find "INFO:" > nul

if errorlevel 1 goto loop

wordpad.exe

这在XP中不起作用。它在Windows 7中运行良好。

1 个答案:

答案 0 :(得分:1)

当任务列表中不存在notepad.exe时,tasklist /fi "imagename eq Notepad.exe"会转储" INFO:"在Windows XP中连接到stderr。您可以使用2>&1将stderr重定向到stdout,但只需更改find /i "notepad"即可。

在旁注中,您可以使用conditional execution而不是if errorlevel 1

@echo off
setlocal

:loop

rem // Output nothing for the following code block.
>NUL 2>NUL (

    rem // Make sure notepad is not running before continuing.
    tasklist /fi "imagename eq notepad.exe" | find /i "notepad" && (

        rem // Notepad is in tasklist.  Sleep 1 second, then check again.
        timeout /t 1 /nobreak || ping -n 2 localhost
        goto loop
    )
)

wordpad.exe