tasklist.exe
does not write to stdErr or record an ErrorLevel you can use, to determine whether the filters returned a task. In order to determine that, you need to use find.exe
or findstr.exe
to check for a known character or string in a successful output. You can then use the returned ErrorLevel or Success/Failure of that to validate instead.
The only 'relatively robust' way to perform this task using tasklist.exe
is to first ensure that you initially ran your batch file, C:
utaejecucion_prueba.bat
using the following command:
Start "?" C:
utaejecucion_prueba.bat
Or (recommended):
Start "?" "C:
utaejecucion_prueba.bat"
With that done, you could run your verification batch file, with the following line in its content:
%SystemRoot%System32asklist.exe /Fi "ImageName Eq cmd.exe" /Fi "Status Eq Running" /Fi "WindowTitle Eq ? - C:
utaejecucion_prueba.bat" | %SystemRoot%System32find.exe "=" 1> NUL || Start "?" "C:
utaejecucion_prueba.bat"
However, if your batch file path contains spaces:
C:
utaejecucion prueba.bat
You'd need to have initially ran it using:
Start "?" "C:
utaejecucion prueba.bat"
Then change the command in your batch script to:
%SystemRoot%System32asklist.exe /Fi "ImageName Eq cmd.exe" /Fi "Status Eq Running" /Fi "WindowTitle Eq ? - "C:
utaejecucion prueba.bat"" | %SystemRoot%System32find.exe "=" 1> NUL || Start "?" "C:
utaejecucion prueba.bat"
Note: Regarding your previous intention to run this indefinitely, (which I do not recommend). When you start
your .bat
file, the Window Title, does not immediately register within tasklist.exe
. That means, were you to run this in a loop or through a scheduled task, it is possible that the delay may cause your script to believe that the batch file isn't running, when in fact it is.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…