Computer Hope

Microsoft => Microsoft DOS => Topic started by: bobthebuilder on December 14, 2008, 10:04:20 PM

Title: Batch file - check if program is running and switch to it
Post by: bobthebuilder on December 14, 2008, 10:04:20 PM
Are there commands to check if a program is already running, and give an external program focus?

Specifically I'm trying to achieve this logic in a batch file?

Code: [Select]
If Firefox is running
       give Firefox program focus and close this batch file
else
        Start firefox, give it focus and close this batch file

Many thanks
Title: Re: Batch file - check if program is running and switch to it
Post by: Sidewinder on December 15, 2008, 05:44:31 AM
Batch files cannot give a Window focus, however they can launch a program which by default will have focus.

Your OS is undefined, so I can only guess that this might work:

Code: [Select]
@echo off
tasklist /fi "imagename eq firefox.exe" > nul
if errorlevel 1 start firefox
exit

Hope this helps.  8)

Note: You may have to fix up any paths required.
Title: Re: Batch file - check if program is running and switch to it
Post by: bobthebuilder on December 18, 2008, 10:50:54 PM
Thanks for your reply, Sidewinder.
I'm WinXP - sorry for not updating during registration!

When I run the tasklist command it in a batch file it doesn't trigger the errorlevel=1 statement. If Firefox is not running, the tasklist command seems to return a 'command cannot be run' style message to NUL and returns 0 as the error code rather than flagging it as an error.

It's been a while since I've done some serious Batch file work, but if I were to send all the output from a tasklist command to a text file, what dos commands could be used to search that file for the text string 'firefox.exe'?
i.e.
Code: [Select]
@echo off
tasklist > temp.txt
[open temp.txt and search for 'firefox.exe' text]
If Firefox text not present start Firefox
exit

Answered my own question with the help of http://www.easywindows.com/easydos/edmessages/560.html

Code: [Select]
@echo off
set tempfile=bdw.txt

del %tempfile%
tasklist > %tempfile%
type %tempfile% | find /i "firefox.exe"
if errorlevel 0 if not errorlevel 1 goto IsRunning
start firefox

:exit
del %tempfile%
pause
exit

:IsRunning
echo IsRunning
goto exit
Title: Re: Batch file - check if program is running and switch to it
Post by: bpoz on June 16, 2009, 11:26:44 AM
Did this work? When I run it, it still tries to launch a new session.
Title: Re: Batch file - check if program is running and switch to it
Post by: ALAN_BR on June 16, 2009, 01:50:40 PM
This works for me to determine whether Nod32kui.exe is running,
and to then take appropriate alternative actions :-

Code: [Select]
TASKLIST /NH | FIND /I "Nod32kui"
SET NodLevel=%ERRORLEVEL%
IF %NodLevel%==0 GOTO END_NORMAL
Subsequently one of the alternative actions is
Code: [Select]
IF %NodLevel% NEQ 0 START /NORMAL C:\PROGRA~1\ESET\nod32kui.exe /WAITSERVICE

Regards
Alan
Title: Re: Batch file - check if program is running and switch to it
Post by: bpoz on June 18, 2009, 12:31:49 PM
Thanks. This seemed to work for me, but what is the command to bring focus or switch to the program?
Title: Re: Batch file - check if program is running and switch to it
Post by: Helpmeh on June 18, 2009, 07:48:28 PM
Thanks. This seemed to work for me, but what is the command to bring focus or switch to the program?
A 3rd party program MIGHT be able to do it, but I doubt it. Batch can't normally do that...
appactivate (or something close to that) in a .wsh file may do it...I found it in the MSDN for SendKeys.
Title: Re: Batch file - check if program is running and switch to it
Post by: uniquegeek on March 31, 2011, 09:42:07 AM
instead of doing "c:\blah.exe" ,
you can do "start c:\blah.exe"

Launched from cmd or bat, the first waits for blah.exe to finish/terminate.
The second says to launch blah in a new window and continue what we're doing.
Title: Re: Batch file - check if program is running and switch to it
Post by: Salmon Trout on March 31, 2011, 10:08:02 AM
instead of doing "c:\blah.exe" ,
you can do "start c:\blah.exe"

You have revived a two year old thread.
Title: Re: Batch file - check if program is running and switch to it
Post by: ihappyk on July 10, 2014, 01:19:13 AM
i think this can also be a alternative.

@echo off

tasklist /nh /fi "imagename eq notepad.exe" | find /i "notepad.exe"
if errorlevel 0 if not errorlevel 1 goto IsRunning
start /b notepad.exe

:exit
pause
exit

:IsRunning
echo Cannot Have Multiple Interface Process Please Check the...
pause
exit
Title: Re: Batch file - check if program is running and switch to it
Post by: foxidrive on July 10, 2014, 05:46:47 AM
This thread was last posted to 3 years ago, and it was created 6 years ago.  Just sayin' :)
Title: Re: Batch file - check if program is running and switch to it
Post by: WayCon on January 16, 2018, 07:43:11 PM
Hello,

I am having a peculiar and specific problem. When I use a batch file to test if, say, "notepad" is working, I use the simple program here:

////////
SETLOCAL EnableExtensions
set EXE=notepad.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Not running
pause
goto FIN
:FOUND
echo Running
pause
:FIN
////////

And it works, of course. But not if I change the second line to:

   set EXE=dbase.exe

This batch file doesn't see that ol' dBase IV is running when it is running.  (I still have the program execute notepad because I don't want the trouble of two versions of dBase fighting over files.) Is there something else I should know about this or is there an alternate test out there? Does this have something to do with running a legacy program like Ashton-Tate's dBase IV? (The program works well except when someone tries to run two versions of it at the same time.)

I thank anyone for their time.
Title: Re: Batch file - check if program is running and switch to it
Post by: patio on January 22, 2018, 05:09:09 PM
Once again...the Thread that will not die...