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

Author Topic: Need .bat to launch 2 programs. P1 exits after P2 is closed.  (Read 4246 times)

0 Members and 1 Guest are viewing this topic.

livemoonkey

    Topic Starter


    Newbie

    • Experience: Experienced
    • OS: Windows 10
    Need .bat to launch 2 programs. P1 exits after P2 is closed.
    « on: February 28, 2018, 05:32:29 PM »
    PLEASE HELP!

    I want to create a batch (.bat) file that:

    1:  Launch two applications.
    2:  Application 1 closes automatically after application 2 is exited manually.

    (Using Windows 10... if this info helps)

    Thank you an advance!

    nil

    • Global Moderator


    • Intermediate
    • Thanked: 15
      • Experience: Experienced
      • OS: Linux variant
      Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
      « Reply #1 on: March 01, 2018, 09:04:25 AM »
      Give this a shot..
      Code: [Select]
      @echo off
      set prog1=mspaint.exe
      set prog2=notepad.exe
      set killcmd=taskkill /f /im

      echo "Starting %prog1% and %prog2%..."
      start %prog1%
      start %prog2%

      :check
      echo Checking if Program2 (%prog2%) is running...
      tasklist /FI "imagename eq %prog2%" | find /I "%prog2%"
      echo %ERRORLEVEL%
      if ERRORLEVEL 1 (
        echo "Program2 (%prog2%) not found in task list. It is not running."
        goto notrunning
        )
      if ERRORLEVEL 0 (
        echo "Program2 (%prog2%) found in tasklist. It is running."
        goto running
        )

      :running
      echo "Waiting before next check.."
      :: Wait 4000ms (4s). Other methods of waiting: http://www.robvanderwoude.com/wait.php
      PING 1.1.1.1 -n 1 -w 4000 >NUL
      goto check

      :notrunning
      echo "Stopping Program1 (%prog1%)..."
      @echo on
      %killcmd% %prog1%
      :: The end
      Do not communicate by sharing memory; instead, share memory by communicating.

      --Effective Go

      livemoonkey

        Topic Starter


        Newbie

        • Experience: Experienced
        • OS: Windows 10
        Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
        « Reply #2 on: March 05, 2018, 06:47:45 PM »
        WOW!  Looks quite complicated! LOL!  Thank you for the quick response!

        Well it worked exactly how I needed it for all but one program.
        The first program which is to close automatically after manually closing the second, doesn't close because of lack of "permission".  I tried giving the exe, and the shortcut and even the folder the program is in administrative privledges and permissions but it still doesn't close.  =(

        I've tried lot's of things.  If you could assist with this I would greatly appreciate it.  Much thanks!

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
        « Reply #3 on: March 05, 2018, 07:28:01 PM »
        Might help a bit to post what app it is...
        " Anyone who goes to a psychiatrist should have his head examined. "

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
        « Reply #4 on: March 05, 2018, 09:49:21 PM »
        The batch file/command prompt wouldn't have permission to kill the task if one of the programs elevates and runs as admin, either itself or through compatibility settings.

        In any case I came up with this version:

        Code: [Select]
        @echo off
        set prog1=notepad.exe
        set prog2=mspaint.exe
        for %%P in (%prog1%) do set prog1name=%%~nP
        start %prog1%
        start /wait %prog2%
        tskill %prog1name%

        (Of course notepad.exe and mspaint.exe are placeholders!). I think start /wait returns immediately sooner if the program self-elevates, as that typically involves relaunching a new instance of the program and exiting the existing one, s o this could have issues there.
        I was trying to dereference Null Pointers before it was cool.

        nil

        • Global Moderator


        • Intermediate
        • Thanked: 15
          • Experience: Experienced
          • OS: Linux variant
          Re: Need .bat to launch 2 programs. P1 exits after P2 is closed.
          « Reply #5 on: March 06, 2018, 10:12:52 AM »
          You might also try running the controlling batch file in an elevated command prompt, e.g. from Start Menu -> expand Windows System -> right-click Command Prompt -> More -> Run as Administrator.

          Do not communicate by sharing memory; instead, share memory by communicating.

          --Effective Go