Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Through a batch file: Check if program is running

Status
Not open for further replies.

mattsv

Programmer
May 21, 2005
28
US
Any way whether through, a third party utility, or an MS executable, or dos shell command which permits checking whether an instance of a program is running from a DOS batch file?

 
I'm sure there is but it would depend on what type of program was running. More information please.
 
Set MyProc=""
FOR /F "tokens=1 delims= " %%A IN ('TASKLIST /V ^¦ FIND /I "process"') DO SET MyPROC=%%A
IF %MyProc"=="" echo The process is not running.

Where process matches the program in question. For example to test if notepad is running:

Set MyProc=""
FOR /F "tokens=1 delims= " %%A IN ('TASKLIST /V ^¦ FIND /I "notpad.exe"') DO SET MyPROC=%%A
IF %MyProc"=="" echo The process is not running.
 
Edit this above:
IF %MyProc"=="" echo The process is not running.

to be:
IF %MyProc%"=="" echo The process is not running.

 
When I run the above, specifically just the portion the FOR loop:

FOR /F "tokens=1 delims= " %%A IN ('TASKLIST /V ^¦ FIND /I "notepad.exe"') DO echo works

I get this error message:
| was unexpected at this time.

I did these steps:
-I corrected "^¦" with ALT 124 (|)

Any thoughts? My workaround is to run tasklist and > to a file, then run find on that file. However, I would be very humble to do this as originally suggested.
 
In a .bat file:

FOR /F "tokens=1 delims= " %%A IN ('TASKLIST /V ^| FIND /I "notepad.exe" ') DO echo works

From a command line:

FOR /F "tokens=1 delims= " %A IN ('TASKLIST /V ^| FIND /I "notepad.exe"') DO echo works

I am not sure why my paste did a broken bar instead of |
So from the beginning again, in a .bat file:

Set MyProc=""
FOR /F "tokens=1 delims= " %%A IN ('TASKLIST /V ^| FIND /I "process_exe"') DO SET MyPROC=%%A
IF "%MyProc"=="" echo The process is not running.

Substitute as needed for process_exe






 
Thanks for the response.

I found my answer. I didn't know that when a pipe character was within quotes it had to be preceeded by an escape character.

Below is a little batch file I use to help show the different cases. Hopefully, someone reading troubled on this will read this post and find this reference to be of use.

Just go through command by command and remove the rem at each line, run the file, and watch the output.


Code:
@echo off

[ol]
[li]
Code:
rem FOR /F "tokens=1 delims= " %%A IN ('tasklist /v ^| c:\windows\system32\FIND /I "notepad"') DO echo carrot pipe
[/li]
[li]
Code:
 rem FOR /F "tokens=1 delims= " %%A IN ('tasklist /v | c:\windows\system32\FIND /I "notepad"') DO echo pipe
[/li]


[li]
Code:
rem TASKLIST /V ^| c:\windows\system32\FIND /I "notepad"
[/li]
[li]
Code:
 rem TASKLIST /V | c:\windows\system32\FIND /I "notepad
[/li]
[/ol]



Here are the results for the code above:

[ol]
[li] Works! output is:
Code:
carrot pipe
[/li]
[li] Fails! output is:
Code:
| was unexpected at this time.
[/li]
[li] Fails! output is:
Code:
ERROR: Invalid Argument/Option - '|'.
Type "TASKLIST /?" for usage.
[/li]
[li] Works! output is:
Code:
notepad.exe [and more text]
[/li]
[/ol]
 
The other "subtle" thing is that placing the tasklist command in single quotes means that it is an executable statement.
If you had used ` or ", the string would have been taken literally.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top