How do I check if a program's instance is running or not, before running that program?

5,474

Solution 1

Something like this, perhaps?

tasklist | find "programname.exe"
if errorlevel 1 c:\path\to\programname.exe

Solution 2

It is almost certainly possible to do in a batch file but (like anything non-trivial) it won't be easy.

However PowerShell makes this easy:

if (-not (Get-Proceess "Name")) {
  #run new instance here
}

where Name is replaced by the process name (this is usually the file name without .exe).

Share:
5,474

Related videos on Youtube

galacticninja
Author by

galacticninja

Updated on September 18, 2022

Comments

  • galacticninja
    galacticninja over 1 year

    I am looking for a software (could be as a BAT script) that could check if a program's instance is running or not, before running that program. And if the software / script detects that the program is not running, it should run that program, else it should not do anything.

    Note: The program I'm gonna be running (uTorrent) doesn't have detection to check if an instance of itself is already running, before running itself.

    I am asking this because I am currently using a software (Networx) which runs uTorrent if it detects that my internet connection's bandwidth has not been used for some time (in my case I set it to run uTorrent if it detects that 50 KB or less has been downloaded and uploaded within a 5 minute-period).

    However, it doesn't have a feature to check if a program is already running before calling it. If it runs uTorrent while uTorrent is already running, uTorrent's window goes to the front of the screen, stealing window focus, which could be distracting if you are working on a window and do not want another window going to the front of the screen.

    The uTorrent /MINIMIZED argument only works correctly if uTorrent is not already running.

    Or does anyone know of another software that can do what the software I'm currently using does, except that it will check if uTorrent is already running, before calling it?

    Edit: I would like to add that it is preferable if the software / script runs minimized or hidden and not go to the foreground or steal window focus. Also, the software or BAT script preferably should exit itself after it has done its task.

  • galacticninja
    galacticninja about 13 years
    Is it correct if I put this in a .CMD file, and not a .BAT file? Also, is there a way to run this CMD/BAT file minimized (window shouldn't appear on the foreground or steal focus when run)?
  • Jason Sherman
    Jason Sherman about 13 years
    It'll function in either a .bat or .cmd file. After tinkering a bit, I think you'll probably want to use the start command like so: if errorlevel 1 start "" "c:\path to\programname.exe" so that the batch will terminate rather than linger. If your launcher program can launch a task minimized, the command interpreter shouldn't have a problem with it.
  • galacticninja
    galacticninja about 13 years
    Turns out I could make a shortcut to the .BAT file and just set it to run 'Minimized' in Properties and run that shortcut instead. I also would like to ask: what is the proper syntax if I would like to run my program with the /MINIMIZED argument (this runs uTorrent minimized)