1

I'm trying to play a wav file when my vpn client disconnects.

Following the advice of Matt Lacey from How to check if a process is running via a batch script

I have the following:

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end
start alarm.wav
:end

When the contents of search.log are empty, alarm.wav plays.
But I don't want it to play when search.log is empty. I want it to play when search.log has info in it such as:

"Image Name","PID","Session Name","Session#","Mem Usage"
"vpngui.exe","2408","Console","0","10,496 K"

Any assistance would greatly be appreciated.

Community
  • 1
  • 1
  • I take it the log file is not actually empty then, even when there's no task running that matches the condition, right? In fact, I even *believe* that the log would contain some text telling you that the requested task was not found. – Andriy M Jan 28 '12 at 15:50
  • @Andriy M - You are definitely correct for Vista (and I presume Windows 7). But based on my reading of the cited Matt Lacey post, it looks like XP produces no output if the process is not found. – dbenham Jan 28 '12 at 16:32
  • @dbenham: Thanks, I wasn't anywhere near a Windows XP installation to test `tasklist` there too, I only tested it in Windows 7. I've tested it in XP now. Both `tasklist` versions print a message when there's no match. The difference is, the Windows XP version prints it to the standard error, while the Windows 7 version prints it to the standard output device. So it's no wonder that the log file appears empty in Windows XP. – Andriy M Jan 28 '12 at 19:32

2 Answers2

1

You could test the actual contents of the log file using the FIND command, and then perform the required action based on the result of the test. Here's how:

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FIND ".exe" < search.log > NUL && start alarm.wav

The FIND command will search for the .exe string in the log file. (You would expect it, wouldn't you, to contain .exe if the previous command did find the requested program in the process list.) The actual line is not important at this moment, so we are discarding the FIND's output (> NUL). What is important is the fact whether the line is found at all. If it is, the command following &&, i.e. start alarm.wav, will be invoked. If not, there will be no alarm.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
1

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)

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • This was a shorter version that eliminated the output file as you stated. Thank you! – user1175312 Jan 28 '12 at 16:40
  • That's a really good idea to get rid of the intermediate file if it's only used to check whether `tasklist` found anything. Can't imagine why I didn't think of it. :) – Andriy M Jan 28 '12 at 19:34