The Matt Lacey solution wants to do something if a process is NOT running. So it checks if the size of the output is 0 length (meaning no process found)
You want to do something if a process IS running, so your logic is inverted. You need to check if the output is <> 0 (meaning a process must have been found)
del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA NEQ 0 GOTO end
start alarm.wav
:end
This can be modified to work without the need of an output file. Note that the output of TASKLIST will be multiple lines if the process is found, but you only want to play the alarm once, hence the GOTO is needed.
for /f %%a in ('tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV') do (
start alarm.wav
goto :break
)
:break
Note - The Matt Lacey solution relies on the fact that the TASKLIST command will not produce any output if it does not find the process. That works fine on XP. But on Vista TASKLIST will produce the following line if no matching process is found - "INFO: No tasks are running which match the specified criteria."
To get the process to work on any version of Windows, you need to do something along the lines of what Andriy M suggests. Here is a variation that eliminates the need for an output file
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV | FIND /I ".exe" >nul && start alarm.wav
The |
is a pipe operator. It causes the output of the TASKLIST command to be piped directly in as input to the FIND command.
(Edit - added the /I
option in case the executable file name uses upper case)