Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file - check if program is running and switch to it  (Read 79922 times)

0 Members and 1 Guest are viewing this topic.

bobthebuilder

    Topic Starter


    Newbie

    Batch file - check if program is running and switch to it
    « 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

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch file - check if program is running and switch to it
    « Reply #1 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.
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    bobthebuilder

      Topic Starter


      Newbie

      Re: Batch file - check if program is running and switch to it
      « Reply #2 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
      « Last Edit: December 18, 2008, 11:27:55 PM by bobthebuilder »

      bpoz



        Newbie

        Re: Batch file - check if program is running and switch to it
        « Reply #3 on: June 16, 2009, 11:26:44 AM »
        Did this work? When I run it, it still tries to launch a new session.

        ALAN_BR



          Hopeful

          Thanked: 5
          • Computer: Specs
          • Experience: Experienced
          • OS: Windows 7
          Re: Batch file - check if program is running and switch to it
          « Reply #4 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

          bpoz



            Newbie

            Re: Batch file - check if program is running and switch to it
            « Reply #5 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?

            Helpmeh



              Guru

            • Roar.
            • Thanked: 123
              • Yes
              • Yes
            • Computer: Specs
            • Experience: Familiar
            • OS: Windows 8
            Re: Batch file - check if program is running and switch to it
            « Reply #6 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.
            Where's MagicSpeed?
            Quote from: 'matt'
            He's playing a game called IRL. Great graphics, *censored* gameplay.

            uniquegeek

            • Guest
            Re: Batch file - check if program is running and switch to it
            « Reply #7 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.

            Salmon Trout

            • Guest
            Re: Batch file - check if program is running and switch to it
            « Reply #8 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.

            ihappyk



              Newbie

              • Experience: Familiar
              • OS: Windows 7
              Re: Batch file - check if program is running and switch to it
              « Reply #9 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

              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              Re: Batch file - check if program is running and switch to it
              « Reply #10 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' :)

              WayCon



                Newbie

                • Experience: Familiar
                • OS: Windows 7
                Re: Batch file - check if program is running and switch to it
                « Reply #11 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.

                patio

                • Moderator


                • Genius
                • Maud' Dib
                • Thanked: 1769
                  • Yes
                • Experience: Beginner
                • OS: Windows 7
                Re: Batch file - check if program is running and switch to it
                « Reply #12 on: January 22, 2018, 05:09:09 PM »
                Once again...the Thread that will not die...
                " Anyone who goes to a psychiatrist should have his head examined. "