Checking if a specific bat file mybatch.bat
is running could be a tougher task than it could look at first sight.
Looking for a particular window title in tasklist /V
as well as testing CommandLine
property in wmic process where "name='cmd.exe'" get CommandLine
might fail under some imaginable circumstance.
1st. Can you
- add
title ThisIsDistinguishingString
command at beginning of the mybatch.bat
and
- remove all other
title
commands from mybatch.bat
and
- ensure that
mybatch.bat
does not call another batch script(s) containing a title
command?
Then check errorlevel
returned from find
command as follows:
:testMybatch
tasklist /V /FI "imagename eq cmd.exe" | find "ThisIsDistinguishingString" > nul
if errorlevel 1 (
rem echo mybatch.bat batch not found
) else (
echo mybatch.bat is running %date% %time%
timeout /T 10 /NOBREAK >NUL 2>&1
goto :testMybatch
)
2nd. Otherwise, check if wmic
Windows Management Instrumentation command output could help
wmic process where "name='cmd.exe'" get /value
Then you could detect mybatch.bat
in its output narrowed to
wmic process where "name='cmd.exe'" get CommandLine, ProcessID
Note that wmic
could return some Win32_Process
class properties, particularly CommandLine
, empty if a particular process was launched under another user account or elevated (run as administrator).
Elevated wmic
returns all properties in full.