Computer Hope
Microsoft => Microsoft DOS => Topic started 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?
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
-
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:
@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.
-
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.
@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
@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
-
Did this work? When I run it, it still tries to launch a new session.
-
This works for me to determine whether Nod32kui.exe is running,
and to then take appropriate alternative actions :-
TASKLIST /NH | FIND /I "Nod32kui"
SET NodLevel=%ERRORLEVEL%
IF %NodLevel%==0 GOTO END_NORMAL
Subsequently one of the alternative actions is
IF %NodLevel% NEQ 0 START /NORMAL C:\PROGRA~1\ESET\nod32kui.exe /WAITSERVICE
Regards
Alan
-
Thanks. This seemed to work for me, but what is the command to bring focus or switch to the program?
-
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.
-
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.
-
instead of doing "c:\blah.exe" ,
you can do "start c:\blah.exe"
You have revived a two year old thread.
-
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
-
This thread was last posted to 3 years ago, and it was created 6 years ago. Just sayin' :)
-
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.
-
Once again...the Thread that will not die...