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

Author Topic: Test to see if Batch File is running  (Read 7037 times)

0 Members and 1 Guest are viewing this topic.

BatchFileCommand

    Topic Starter


    Hopeful
  • Thanked: 1
    Test to see if Batch File is running
    « on: February 27, 2009, 07:41:11 PM »
    In my program, I want to be able to test if the same batch file is running. If so, I want it to shut down the current session. How would you do this?
    οτη άβγαλτος μεταφ βαθμολογία

    macdad-



      Expert

      Thanked: 40
      Re: Test to see if Batch File is running
      « Reply #1 on: February 28, 2009, 10:34:22 AM »
      you'd probably use Tasklist and Taskill.
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      BatchFileCommand

        Topic Starter


        Hopeful
      • Thanked: 1
        Re: Test to see if Batch File is running
        « Reply #2 on: February 28, 2009, 11:16:48 AM »
        I can't see how to do this. Could I have a little more help.
        οτη άβγαλτος μεταφ βαθμολογία

        macdad-



          Expert

          Thanked: 40
          Re: Test to see if Batch File is running
          « Reply #3 on: February 28, 2009, 12:05:15 PM »
          actually, i tested this and no you could do this since even if you title your batch program, it would still show up as CMD.EXE in tasklist.
          If you dont know DOS, you dont know Windows...

          Thats why Bill Gates created the Windows NT Family.

          BatchFileCommand

            Topic Starter


            Hopeful
          • Thanked: 1
            Re: Test to see if Batch File is running
            « Reply #4 on: February 28, 2009, 12:07:43 PM »
            So, I'm still trying to figure something out. How would you test to see if it is running in Task list.
            οτη άβγαλτος μεταφ βαθμολογία

            macdad-



              Expert

              Thanked: 40
              If you dont know DOS, you dont know Windows...

              Thats why Bill Gates created the Windows NT Family.

              BatchFileCommand

                Topic Starter


                Hopeful
              • Thanked: 1
                Re: Test to see if Batch File is running
                « Reply #6 on: February 28, 2009, 12:15:44 PM »
                So, how would you combine the status into the tasklist statement.
                οτη άβγαλτος μεταφ βαθμολογία

                macdad-



                  Expert

                  Thanked: 40
                  Re: Test to see if Batch File is running
                  « Reply #7 on: February 28, 2009, 12:39:12 PM »
                  pipe it
                  If you dont know DOS, you dont know Windows...

                  Thats why Bill Gates created the Windows NT Family.

                  BatchFileCommand

                    Topic Starter


                    Hopeful
                  • Thanked: 1
                    Re: Test to see if Batch File is running
                    « Reply #8 on: February 28, 2009, 05:18:59 PM »
                    Meaning?
                    οτη άβγαλτος μεταφ βαθμολογία

                    devcom



                      Apprentice

                      Thanked: 37
                      Re: Test to see if Batch File is running
                      « Reply #9 on: March 01, 2009, 03:20:27 AM »
                      i would do this like this :

                      Code: [Select]
                      @echo off
                      set title=Title 1234

                      title Test 1234

                      tasklist /V >nul | findstr "%username%:  %title%" && echo Cmd with title: %title% is running || echo Cmd with title: %title% is not running
                      pause
                      Download: Choice.exe

                      BatchFileCommand

                        Topic Starter


                        Hopeful
                      • Thanked: 1
                        Re: Test to see if Batch File is running
                        « Reply #10 on: March 01, 2009, 08:40:20 AM »
                        Hm.. Didn't work.
                        οτη άβγαλτος μεταφ βαθμολογία

                        Dias de verano

                        • Guest
                        Re: Test to see if Batch File is running
                        « Reply #11 on: March 01, 2009, 09:09:45 AM »
                        Hm.. Didn't work.

                        How about this?

                        Code: [Select]
                        @echo off
                        set title=Title 1234
                        title %title%
                        tasklist /v | find "%title%">nul
                        if %errorlevel% EQU 0 (
                        set state=running
                        ) else (
                        set state=not running
                        )
                        echo Task with title %title% is %state%
                        pause

                        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: Test to see if Batch File is running
                        « Reply #12 on: March 01, 2009, 09:38:37 AM »
                        Or, you could write a program that uses the EnumWindows() routine to enumerate all windows, and for each window call
                        GetWindowThreadProcessId, and then use the resulting ProcessID in a call to GetModuleBasename, and after determining that the window belongs to CMD, proceed to use OpenProcess() and ReadProcessMemory() to determine the opened file handles of the CMD.EXE instance, and if you find your batch file, kill the current instance and otherwise continue.

                        Really quite straightforward really. Although I suppose Openprocess() will fail anyway, and ReadProcessMemory() is a fairly big gamble  on pretty much any NT system. Oh well.
                        I was trying to dereference Null Pointers before it was cool.

                        BatchFileCommand

                          Topic Starter


                          Hopeful
                        • Thanked: 1
                          Re: Test to see if Batch File is running
                          « Reply #13 on: March 01, 2009, 03:16:41 PM »
                          Quote
                          How about this?

                          Code: [Select]
                          @echo off
                          set title=Title 1234
                          title %title%
                          tasklist /v | find "%title%">nul
                          if %errorlevel% EQU 0 (
                          set state=running
                          ) else (
                          set state=not running
                          )
                          echo Task with title %title% is %state%
                          pause



                          It always returns and says it's running, I echoed the errorlevel, and it always returned 0, now It's also possible that because it's looking to see if it's running itself it will always be found. However, the batch file is listed as cmd.exe.
                          οτη άβγαλτος μεταφ βαθμολογία

                          Dias de verano

                          • Guest
                          Re: Test to see if Batch File is running
                          « Reply #14 on: March 01, 2009, 03:25:47 PM »
                          It always returns and says it's running

                          Of course it does! (How could it do otherwise?) It's a demo to show you how to look for a running batch by its window title.


                          BatchFileCommand

                            Topic Starter


                            Hopeful
                          • Thanked: 1
                            Re: Test to see if Batch File is running
                            « Reply #15 on: March 01, 2009, 03:45:58 PM »
                            Oh, I was thinking to see if there are to sessions of cmd.exe running.
                            οτη άβγαλτος μεταφ βαθμολογία

                            oldun

                            • Guest
                            Re: Test to see if Batch File is running
                            « Reply #16 on: March 01, 2009, 08:56:21 PM »
                            Maybe the batch file could create a file when it starts, and delete it when it completes.

                            The batch file could then check for the existence of that file when it starts, and if it finds the file, it would exit, or else it would carry on.

                            BatchFileCommand

                              Topic Starter


                              Hopeful
                            • Thanked: 1
                              Re: Test to see if Batch File is running
                              « Reply #17 on: March 02, 2009, 07:25:47 PM »
                              That would be a lot of work. Because the only way that you could really quit would to click the exit button in the corner of the screen. Or doing quit. But the person will probably press the X button.
                              οτη άβγαλτος μεταφ βαθμολογία

                              Dias de verano

                              • Guest
                              Re: Test to see if Batch File is running
                              « Reply #18 on: March 03, 2009, 12:16:29 AM »
                              the only way that you could really quit would to click the exit button in the corner of the screen. Or doing quit.

                              A batch file will quit when it encounters the EXIT command.  ::)

                              oldun

                              • Guest
                              Re: Test to see if Batch File is running
                              « Reply #19 on: March 03, 2009, 12:29:45 AM »
                              I may have misunderstood your requirements, but what I was thinking of was something like this:


                              @Echo Off
                              :: Check if temporary file exists
                              If Exist %temp%\AlreadyRunning.tmp (
                                 Echo This batch file is already running & Goto :EOF)
                              :: Else create it
                              Echo . > %temp%\AlreadyRunning.tmp
                              :: Carry on with remainder of batch file
                              Echo.
                              Echo Do whatever here, then
                              Echo.
                              :: Delete temporary file
                              Del %temp%\AlreadyRunning.tmp


                              BatchFileCommand

                                Topic Starter


                                Hopeful
                              • Thanked: 1
                                Re: Test to see if Batch File is running
                                « Reply #20 on: March 03, 2009, 07:57:08 PM »
                                My program. Doesn't really have a beginning and an end. It has a old GUI with options.
                                οτη άβγαλτος μεταφ βαθμολογία

                                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: Test to see if Batch File is running
                                « Reply #21 on: March 03, 2009, 09:08:31 PM »
                                My program. Doesn't really have a beginning and an end. It has a old GUI with options.

                                all programs have a beginning and an end.  instead of exiting the batch file using EXIT, you can redirect to a label that performs the necessary cleanup.
                                I was trying to dereference Null Pointers before it was cool.

                                BatchFileCommand

                                  Topic Starter


                                  Hopeful
                                • Thanked: 1
                                  Re: Test to see if Batch File is running
                                  « Reply #22 on: March 04, 2009, 06:03:31 AM »
                                  Even if it does have a beginning and an end. What if someone doesn't know about the file and clicks the X button in the corner of the screen. Then they would never be able to use it again.
                                  οτη άβγαλτος μεταφ βαθμολογία

                                  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: Test to see if Batch File is running
                                  « Reply #23 on: March 04, 2009, 06:07:05 AM »
                                  This is why I stopped using this method in my batch files/programs early on.
                                  I was trying to dereference Null Pointers before it was cool.