1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Batch file closing if the same one is running

Discussion in 'Software Development' started by tehdansorz, May 3, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. tehdansorz

    tehdansorz Thread Starter

    Joined:
    Feb 6, 2011
    Messages:
    87
    Hey , i really need help with this - how to make a batch file close itself if the same one is already running ? I need this so my users don't open the same program multiple times . If you want to check what i'm doing PM me or ask me here .
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    What you would want to do at the top of your batch file is use the TASKLIST program to find cmd.exe with the window title of your batch file. IF it finds it then exit your batch file.
    Then after that set the Title of your batch file

    So do something like this at the top of your batch file

    Code:
    for /F "tokens=1" %%G in ('tasklist /V /NH /FO TABLE /FI "WINDOWTITLE eq BATCH1"') do IF %%G==cmd.exe exit /b
    TITLE BATCH1
    First time it runs it will not find BATCH1
    Second time it runs it should exit the batch file.
     
  3. tehdansorz

    tehdansorz Thread Starter

    Joined:
    Feb 6, 2011
    Messages:
    87
    Thanks ! Just the answer i needed :)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1051824