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 7035 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.