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

Author Topic: Multiple batch file option  (Read 3231 times)

0 Members and 1 Guest are viewing this topic.

Gajananpund

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    Multiple batch file option
    « on: October 03, 2011, 03:16:24 AM »
    Hi,

    I have multiple batch files in a folder ,i want find out if batch A is running then batch B should not run and vice versa.

    Any type of help will be appriciated.

    regards,

    Gajanan

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Multiple batch file option
    « Reply #1 on: October 03, 2011, 01:44:51 PM »
    Scripts and batch files have interpreters for execution and do nor run as processes. This snippet shows a method for querying the window title but is not idiot proof. Depending how the batch file is launched will determine the actual window title and there are too many variations for a definitive response.

    Code: [Select]
    @echo off
    setlocal

    for /f %%i in ('tasklist /nh /fi "WINDOWTITLE eq Command Prompt - BatchA" ^| find /i "cmd.exe"') do (
      if errorlevel 1 (echo BatchA is NOT running
      ) else (echo BatchA is Running)


    VBScript or Powershell would probably give you a better and more elegant solution.

    Good luck.  8)

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Multiple batch file option
    « Reply #2 on: October 03, 2011, 03:42:34 PM »
    It's too early for All Hallows Eve but something is about. When re-testing the code, it seems it might not work. Actually it screams, "IT WILL NOT WORK".

    Try this more better code:

    Code: [Select]
    @echo off
    setlocal

    for /f %%i in ('tasklist /nh /fi "WINDOWTITLE eq Command Prompt - BatchA"') do (
      echo %%i | find /i "cmd.exe" > nul
      if errorlevel 1 (echo BatchA is NOT running
      ) else echo BatchA is running)


     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: Multiple batch file option
    « Reply #3 on: October 03, 2011, 05:01:14 PM »
     ;D
    " Anyone who goes to a psychiatrist should have his head examined. "

    Gajananpund

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Unknown
      Re: Multiple batch file option
      « Reply #4 on: October 03, 2011, 11:02:35 PM »
      Thank you very much its working for.
      but if BatchA is running then it should exit from BatchB and viceversa.
      so it will be very helpful if you provide me that code.

      Salmon Trout

      • Guest
      Re: Multiple batch file option
      « Reply #5 on: October 04, 2011, 12:13:21 AM »
      so it will be very helpful if you provide me that code.

      Surely you can work it out for yourself?

      Raven19528



        Hopeful
      • Thanked: 30
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: Multiple batch file option
        « Reply #6 on: October 06, 2011, 11:04:43 PM »
        Substitute:

        if errorlevel 1...

        for...

        if errorlevel 0 goto eof

        Surely you can work it out for yourself?

        Sometimes.........it just doesn't make sense.  :'(
        "All things that are
        Are with more spirit chased than enjoy'd" -Shakespeare

        Salmon Trout

        • Guest
        Re: Multiple batch file option
        « Reply #7 on: October 07, 2011, 12:14:58 AM »
        Personally what I might do is make each batch script create a lock file after first checking for the existence of the other script's lock file and exiting if it is found. Or you could make it wait in a loop until the other script's lock file becomes non-existent. If it is not found, then the script creates its own lock file, performs it operations, then finally deletes its lock file.

        e.g.

        REM Batch A
        if exist batchB.lock exit
        echo abcde > batchA.lock
        bla bla
        bla bla
        bla bla
        del batchA.lock

        REM Batch B
        if exist batchA.lock exit
        echo abcde > batchB.lock
        bla bla
        bla bla
        bla bla
        del batchB.lock