# MS-Dos Batch File Check if Program is running and exceute



## Boylett

I have just started trying to do some code for MS-Dos Batch files. I have written a few minor things like, returning someones text they enter. But I have been searching all over google and I can't find out how to do this:

How can I make it so every 5 minutes, the MS-Dos Batch file will check if a .exe file is running, if it is, leave it and starting counting again, and, if it isnt, start it and start counting again.

Thanks  

p.s. nice christmas smilies


----------



## Chicon

Hi Boylett,

Welcome to TSG ! 

You should first download the *Windows 2003 Resource Kit* from here.
There is a tool called *pmon.exe* that allows you to view the running processes in a DOS window.

Also, add a look to the MS-DOS *at* command to schedule tasks to be performed at a specified time and date.


----------



## Boylett

Ok, I have downloaded the Windows 2003 Tool Kit. When I looked at pmon.exe, yes it told me which programs were running, but how would I go about doing a command which asks it whether a program is running?


----------



## Chicon

Example :
-------



Code:


[SIZE=2]pmon.exe > result.txt
find "your_process.exe" result.txt
if errorlevel 0 goto found
...
...
...
:found
...
...[/SIZE]


----------



## Boylett

Its writes nothing to result.txt


----------



## Chicon

- Dowload Process Viewer for Windows from here,

- Extract the file into a folder anywhere on your drive,

- Browse the folder and you'll see a file called *pv.exe*,

- Via a DOS windows : *cd* until you reach the folder then type *pv > result.txt* and you will have all the running processes in the file result.txt (it works).

The *pmon.exe* is aimed to build a batch file, you must trigger to show the processes.

Also, you may copy paste the *pv.exe* file into the folder where the MS-DOS executables are stored (*C:\WINDOWS\system32* if you've a standard installation of Windows XP).


----------



## Boylett

ok, still a few problems 



Code:


@echo off

:BEGIN

cd G:\pv
START pv.exe

pv > result.txt

find "prog.exe" result.txt
if errorlevel 0 goto BEGIN

cd G:\

START prog.exe

goto BEGIN

if this line: *if errorlevel 0 goto BEGIN* is *if errorlevel 1 goto BEGIN* it constanly starts program, if its how it is, it never starts it. So its still not working >.<


----------



## Chicon

I have tested the following code and it works :



Code:


[SIZE=2]
@ECHO off

:BEGIN

%SystemRoot%\Temp\pv.exe -d10000 > result.txt

FIND "notepad.exe" result.txt
IF ERRORLEVEL 1 %SystemRoot%\system32\notepad.exe

GOTO BEGIN
[/SIZE]

The *-d10000* parameter of *pv.exe* means a delay of 10 seconds.
After the *FIND* command, the test of *ERRORLEVEL* must be retricted to the value *1*.
Indeed, the value *0* is always produced weither the string was found or not !


----------



## Boylett

it writes nothing to result.txt, still, so constantly starts it for me ???


----------



## Chicon

Duh ! My previous code seems working but in fact, it's waiting the Notepad process to be closed. Also, the delay parameter of the process viewer doesn't produce any result. 

Now, I assure you the following piece of code is working : this time, I've tested it step by step and the result.txt file is not empty :



Code:


[SIZE=2]
@ECHO off

:BEGIN

%SystemRoot%\Temp\pv.exe -d10000

%SystemRoot%\Temp\pv.exe > result.txt

FIND "notepad.exe" result.txt
IF ERRORLEVEL 1 START %SystemRoot%\system32\notepad.exe

GOTO BEGIN
[/SIZE]


----------



## Boylett

Thanks, its all working now


----------



## Chicon

Boylett said:


> Thanks, its all working now


You're welcome !


----------

