Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Development
  4. Check if .exe-file, WITH A PATH(!!!), is running, from within a bat-file?

Check if .exe-file, WITH A PATH(!!!), is running, from within a bat-file?

Scheduled Pinned Locked Moved Windows Development
debugginghelptutorialquestionannouncement
10 Posts 3 Posters 22 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    When I search on the Internet, I find millions of web pages explaining how to check if a .exe-file is running, from within a .bat-file. However, I can't find a single page explaining what to do if you have several .exe-files with identical names and you only want to check an .exe-file that resides in a specific folder. Can anybody please help me, what's the way to detect if the file C:\MyProject\bin\release\MyApplication.exe is running, but at the same time completely ignore C:\MyProject\bin\debug\MyApplication.exe?

    L V A 3 Replies Last reply
    0
    • A arnold_w

      When I search on the Internet, I find millions of web pages explaining how to check if a .exe-file is running, from within a .bat-file. However, I can't find a single page explaining what to do if you have several .exe-files with identical names and you only want to check an .exe-file that resides in a specific folder. Can anybody please help me, what's the way to detect if the file C:\MyProject\bin\release\MyApplication.exe is running, but at the same time completely ignore C:\MyProject\bin\debug\MyApplication.exe?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      In the absence of a language specification: [GetProcessImageFileNameA function (psapi.h) - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getprocessimagefilenamea)

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      1 Reply Last reply
      0
      • A arnold_w

        When I search on the Internet, I find millions of web pages explaining how to check if a .exe-file is running, from within a .bat-file. However, I can't find a single page explaining what to do if you have several .exe-files with identical names and you only want to check an .exe-file that resides in a specific folder. Can anybody please help me, what's the way to detect if the file C:\MyProject\bin\release\MyApplication.exe is running, but at the same time completely ignore C:\MyProject\bin\debug\MyApplication.exe?

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        Using EnumProcesses / OpenProcess / GetModuleFileName you can get the full path names of all the running exe files. See example: [Enumerating All Processes - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/psapi/enumerating-all-processes) BTW, what do you mean by "... from within a .bat-file."?

        A 1 Reply Last reply
        0
        • V Victor Nijegorodov

          Using EnumProcesses / OpenProcess / GetModuleFileName you can get the full path names of all the running exe files. See example: [Enumerating All Processes - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/psapi/enumerating-all-processes) BTW, what do you mean by "... from within a .bat-file."?

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #4

          Victor Nijegorodov wrote:

          BTW, what do you mean by "... from within a .bat-file."?

          I want to do it in bat-file language (the language you type in the command prompt C:\Windows\System32\cmd.exe)

          1 Reply Last reply
          0
          • A arnold_w

            When I search on the Internet, I find millions of web pages explaining how to check if a .exe-file is running, from within a .bat-file. However, I can't find a single page explaining what to do if you have several .exe-files with identical names and you only want to check an .exe-file that resides in a specific folder. Can anybody please help me, what's the way to detect if the file C:\MyProject\bin\release\MyApplication.exe is running, but at the same time completely ignore C:\MyProject\bin\debug\MyApplication.exe?

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            @echo off

            set "workdir=C:\MyProject\bin\release"
            set "workdir=%workdir:\=\\%"

            setlocal enableDelayedExpansion
            for /f "usebackq tokens=* delims=" %%a in (`
            wmic process where 'CommandLine like "%%!workdir!%%" and not CommandLine like "%%RuntimeBroker%%"' get CommandLine^,ProcessId /format:value
            `) do (
            for /f "tokens=* delims=" %%G in ("%%a") do (
            if "%%G" neq "" (
            rem echo %%G
            set "%%G"
            rem echo !ProcessId!
            goto :TheApplicationIsRunning
            )
            )
            )

            echo The application is not running
            exit /B

            :TheApplicationIsRunning
            echo The application is running
            exit /B

            V L 2 Replies Last reply
            0
            • A arnold_w

              @echo off

              set "workdir=C:\MyProject\bin\release"
              set "workdir=%workdir:\=\\%"

              setlocal enableDelayedExpansion
              for /f "usebackq tokens=* delims=" %%a in (`
              wmic process where 'CommandLine like "%%!workdir!%%" and not CommandLine like "%%RuntimeBroker%%"' get CommandLine^,ProcessId /format:value
              `) do (
              for /f "tokens=* delims=" %%G in ("%%a") do (
              if "%%G" neq "" (
              rem echo %%G
              set "%%G"
              rem echo !ProcessId!
              goto :TheApplicationIsRunning
              )
              )
              )

              echo The application is not running
              exit /B

              :TheApplicationIsRunning
              echo The application is running
              exit /B

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              Is it your solution?

              A 1 Reply Last reply
              0
              • V Victor Nijegorodov

                Is it your solution?

                A Offline
                A Offline
                arnold_w
                wrote on last edited by
                #7

                Yes

                V 1 Reply Last reply
                0
                • A arnold_w

                  Yes

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  Congrats! :)

                  A 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    Congrats! :)

                    A Offline
                    A Offline
                    arnold_w
                    wrote on last edited by
                    #9

                    Thank you!

                    1 Reply Last reply
                    0
                    • A arnold_w

                      @echo off

                      set "workdir=C:\MyProject\bin\release"
                      set "workdir=%workdir:\=\\%"

                      setlocal enableDelayedExpansion
                      for /f "usebackq tokens=* delims=" %%a in (`
                      wmic process where 'CommandLine like "%%!workdir!%%" and not CommandLine like "%%RuntimeBroker%%"' get CommandLine^,ProcessId /format:value
                      `) do (
                      for /f "tokens=* delims=" %%G in ("%%a") do (
                      if "%%G" neq "" (
                      rem echo %%G
                      set "%%G"
                      rem echo !ProcessId!
                      goto :TheApplicationIsRunning
                      )
                      )
                      )

                      echo The application is not running
                      exit /B

                      :TheApplicationIsRunning
                      echo The application is running
                      exit /B

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      That's a lot of magic :D If prefer to write my own.

                      Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups