Run a process and return its PID (as CHP.EXE) for a batch file











up vote
6
down vote

favorite
1












I'm looking for something like chp.exe, it can runs a command and returns its pid.
I can not use CHP.EXE because it seems to be reported as infected file in some antiviruses.



Thanks










share|improve this question




















  • 1




    You can run the program and parse the output of tasklist.
    – Karan
    May 20 '13 at 15:43

















up vote
6
down vote

favorite
1












I'm looking for something like chp.exe, it can runs a command and returns its pid.
I can not use CHP.EXE because it seems to be reported as infected file in some antiviruses.



Thanks










share|improve this question




















  • 1




    You can run the program and parse the output of tasklist.
    – Karan
    May 20 '13 at 15:43















up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





I'm looking for something like chp.exe, it can runs a command and returns its pid.
I can not use CHP.EXE because it seems to be reported as infected file in some antiviruses.



Thanks










share|improve this question















I'm looking for something like chp.exe, it can runs a command and returns its pid.
I can not use CHP.EXE because it seems to be reported as infected file in some antiviruses.



Thanks







batch pid






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 5 '14 at 6:11

























asked May 20 '13 at 15:16









Tobia

1,06471638




1,06471638








  • 1




    You can run the program and parse the output of tasklist.
    – Karan
    May 20 '13 at 15:43
















  • 1




    You can run the program and parse the output of tasklist.
    – Karan
    May 20 '13 at 15:43










1




1




You can run the program and parse the output of tasklist.
– Karan
May 20 '13 at 15:43






You can run the program and parse the output of tasklist.
– Karan
May 20 '13 at 15:43












4 Answers
4






active

oldest

votes

















up vote
4
down vote



accepted










Wmic process where (Name like '%CHP%') get caption, name, commandline, ProcessId | more


real sample:



Wmic process where (Name like '%ie%') get caption, name, commandline, ProcessId | more


Output:



ProcessId where like name Wmic Far manager



Wmic process get ProcessId


extract only the processId into a variable from query output:



ProcID.cmd:



@ECHO OFF                                                                              
FOR /F %%T IN ('Wmic process where^(Name^="explorer.exe"^)get ProcessId^|more +1') DO (
SET /A ProcessId=%%T) &GOTO SkipLine
:SkipLine
echo ProcessId = %ProcessId%


Output:



ProcessId = 2372    





share|improve this answer























  • any suggestion to extract only the processId into a variable from query output?
    – Tobia
    May 28 '13 at 7:22










  • @Tobia see again
    – STTR
    May 28 '13 at 9:44


















up vote
2
down vote













This is pretty straightforward to do in PowerShell. Start-Process notepad.exe -PassThru will start Notepad and return:



Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                              
------- ------ ----- ----- ----- ------ -- -----------
45 6 1672 4240 61 0.02 9212 notepad


From there you could use it like an object by storing the output ($notepad = Start-process notepad.exe -PassThru and then $notepad.ID) and finish whatever scripting you were doing with it.



Getting it back to a batch script (if absolutely necessary) is a little tricky. If you're absolutely stuck on using a batch file, it would probably be easiest to write a PowerShell script that writes the relevant information to a file or registry key, call the PowerShell script from the batch file, and then read the file or reg key later in the batch script.



If you don't want to touch PowerShell at all then you'll have to resort to WMIC or tasklist to find the process after creating it.






share|improve this answer




























    up vote
    0
    down vote













    you can run the program and then issue this command, substituting firefox with the process name you want:



    for /f "tokens=2" %a in ('tasklist /nh /fi "imagename eq firefox.exe"') do echo %a


    it will output the pid solely, in my case, the output is 5540






    share|improve this answer




























      up vote
      0
      down vote













      Another way :: You can create a batch file and put following code to batch file. Its first run netstat command to get processId for port 9797. And then set it into a variable.



      @ECHO OFF                                                                              
      FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
      SET /A ProcessId=%%T) &GOTO SkipLine
      :SkipLine
      echo ProcessId = %ProcessId%
      PAUSE





      share|improve this answer























      • Please explain what this is doing.
        – Scott
        Dec 5 at 8:12











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f597726%2frun-a-process-and-return-its-pid-as-chp-exe-for-a-batch-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      Wmic process where (Name like '%CHP%') get caption, name, commandline, ProcessId | more


      real sample:



      Wmic process where (Name like '%ie%') get caption, name, commandline, ProcessId | more


      Output:



      ProcessId where like name Wmic Far manager



      Wmic process get ProcessId


      extract only the processId into a variable from query output:



      ProcID.cmd:



      @ECHO OFF                                                                              
      FOR /F %%T IN ('Wmic process where^(Name^="explorer.exe"^)get ProcessId^|more +1') DO (
      SET /A ProcessId=%%T) &GOTO SkipLine
      :SkipLine
      echo ProcessId = %ProcessId%


      Output:



      ProcessId = 2372    





      share|improve this answer























      • any suggestion to extract only the processId into a variable from query output?
        – Tobia
        May 28 '13 at 7:22










      • @Tobia see again
        – STTR
        May 28 '13 at 9:44















      up vote
      4
      down vote



      accepted










      Wmic process where (Name like '%CHP%') get caption, name, commandline, ProcessId | more


      real sample:



      Wmic process where (Name like '%ie%') get caption, name, commandline, ProcessId | more


      Output:



      ProcessId where like name Wmic Far manager



      Wmic process get ProcessId


      extract only the processId into a variable from query output:



      ProcID.cmd:



      @ECHO OFF                                                                              
      FOR /F %%T IN ('Wmic process where^(Name^="explorer.exe"^)get ProcessId^|more +1') DO (
      SET /A ProcessId=%%T) &GOTO SkipLine
      :SkipLine
      echo ProcessId = %ProcessId%


      Output:



      ProcessId = 2372    





      share|improve this answer























      • any suggestion to extract only the processId into a variable from query output?
        – Tobia
        May 28 '13 at 7:22










      • @Tobia see again
        – STTR
        May 28 '13 at 9:44













      up vote
      4
      down vote



      accepted







      up vote
      4
      down vote



      accepted






      Wmic process where (Name like '%CHP%') get caption, name, commandline, ProcessId | more


      real sample:



      Wmic process where (Name like '%ie%') get caption, name, commandline, ProcessId | more


      Output:



      ProcessId where like name Wmic Far manager



      Wmic process get ProcessId


      extract only the processId into a variable from query output:



      ProcID.cmd:



      @ECHO OFF                                                                              
      FOR /F %%T IN ('Wmic process where^(Name^="explorer.exe"^)get ProcessId^|more +1') DO (
      SET /A ProcessId=%%T) &GOTO SkipLine
      :SkipLine
      echo ProcessId = %ProcessId%


      Output:



      ProcessId = 2372    





      share|improve this answer














      Wmic process where (Name like '%CHP%') get caption, name, commandline, ProcessId | more


      real sample:



      Wmic process where (Name like '%ie%') get caption, name, commandline, ProcessId | more


      Output:



      ProcessId where like name Wmic Far manager



      Wmic process get ProcessId


      extract only the processId into a variable from query output:



      ProcID.cmd:



      @ECHO OFF                                                                              
      FOR /F %%T IN ('Wmic process where^(Name^="explorer.exe"^)get ProcessId^|more +1') DO (
      SET /A ProcessId=%%T) &GOTO SkipLine
      :SkipLine
      echo ProcessId = %ProcessId%


      Output:



      ProcessId = 2372    






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 28 '13 at 9:49

























      answered May 20 '13 at 15:38









      STTR

      5,82311217




      5,82311217












      • any suggestion to extract only the processId into a variable from query output?
        – Tobia
        May 28 '13 at 7:22










      • @Tobia see again
        – STTR
        May 28 '13 at 9:44


















      • any suggestion to extract only the processId into a variable from query output?
        – Tobia
        May 28 '13 at 7:22










      • @Tobia see again
        – STTR
        May 28 '13 at 9:44
















      any suggestion to extract only the processId into a variable from query output?
      – Tobia
      May 28 '13 at 7:22




      any suggestion to extract only the processId into a variable from query output?
      – Tobia
      May 28 '13 at 7:22












      @Tobia see again
      – STTR
      May 28 '13 at 9:44




      @Tobia see again
      – STTR
      May 28 '13 at 9:44












      up vote
      2
      down vote













      This is pretty straightforward to do in PowerShell. Start-Process notepad.exe -PassThru will start Notepad and return:



      Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                              
      ------- ------ ----- ----- ----- ------ -- -----------
      45 6 1672 4240 61 0.02 9212 notepad


      From there you could use it like an object by storing the output ($notepad = Start-process notepad.exe -PassThru and then $notepad.ID) and finish whatever scripting you were doing with it.



      Getting it back to a batch script (if absolutely necessary) is a little tricky. If you're absolutely stuck on using a batch file, it would probably be easiest to write a PowerShell script that writes the relevant information to a file or registry key, call the PowerShell script from the batch file, and then read the file or reg key later in the batch script.



      If you don't want to touch PowerShell at all then you'll have to resort to WMIC or tasklist to find the process after creating it.






      share|improve this answer

























        up vote
        2
        down vote













        This is pretty straightforward to do in PowerShell. Start-Process notepad.exe -PassThru will start Notepad and return:



        Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                              
        ------- ------ ----- ----- ----- ------ -- -----------
        45 6 1672 4240 61 0.02 9212 notepad


        From there you could use it like an object by storing the output ($notepad = Start-process notepad.exe -PassThru and then $notepad.ID) and finish whatever scripting you were doing with it.



        Getting it back to a batch script (if absolutely necessary) is a little tricky. If you're absolutely stuck on using a batch file, it would probably be easiest to write a PowerShell script that writes the relevant information to a file or registry key, call the PowerShell script from the batch file, and then read the file or reg key later in the batch script.



        If you don't want to touch PowerShell at all then you'll have to resort to WMIC or tasklist to find the process after creating it.






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          This is pretty straightforward to do in PowerShell. Start-Process notepad.exe -PassThru will start Notepad and return:



          Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                              
          ------- ------ ----- ----- ----- ------ -- -----------
          45 6 1672 4240 61 0.02 9212 notepad


          From there you could use it like an object by storing the output ($notepad = Start-process notepad.exe -PassThru and then $notepad.ID) and finish whatever scripting you were doing with it.



          Getting it back to a batch script (if absolutely necessary) is a little tricky. If you're absolutely stuck on using a batch file, it would probably be easiest to write a PowerShell script that writes the relevant information to a file or registry key, call the PowerShell script from the batch file, and then read the file or reg key later in the batch script.



          If you don't want to touch PowerShell at all then you'll have to resort to WMIC or tasklist to find the process after creating it.






          share|improve this answer












          This is pretty straightforward to do in PowerShell. Start-Process notepad.exe -PassThru will start Notepad and return:



          Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                              
          ------- ------ ----- ----- ----- ------ -- -----------
          45 6 1672 4240 61 0.02 9212 notepad


          From there you could use it like an object by storing the output ($notepad = Start-process notepad.exe -PassThru and then $notepad.ID) and finish whatever scripting you were doing with it.



          Getting it back to a batch script (if absolutely necessary) is a little tricky. If you're absolutely stuck on using a batch file, it would probably be easiest to write a PowerShell script that writes the relevant information to a file or registry key, call the PowerShell script from the batch file, and then read the file or reg key later in the batch script.



          If you don't want to touch PowerShell at all then you'll have to resort to WMIC or tasklist to find the process after creating it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 20 '13 at 15:43









          Tanner Faulkner

          9,055134281




          9,055134281






















              up vote
              0
              down vote













              you can run the program and then issue this command, substituting firefox with the process name you want:



              for /f "tokens=2" %a in ('tasklist /nh /fi "imagename eq firefox.exe"') do echo %a


              it will output the pid solely, in my case, the output is 5540






              share|improve this answer

























                up vote
                0
                down vote













                you can run the program and then issue this command, substituting firefox with the process name you want:



                for /f "tokens=2" %a in ('tasklist /nh /fi "imagename eq firefox.exe"') do echo %a


                it will output the pid solely, in my case, the output is 5540






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  you can run the program and then issue this command, substituting firefox with the process name you want:



                  for /f "tokens=2" %a in ('tasklist /nh /fi "imagename eq firefox.exe"') do echo %a


                  it will output the pid solely, in my case, the output is 5540






                  share|improve this answer












                  you can run the program and then issue this command, substituting firefox with the process name you want:



                  for /f "tokens=2" %a in ('tasklist /nh /fi "imagename eq firefox.exe"') do echo %a


                  it will output the pid solely, in my case, the output is 5540







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 5 '14 at 6:30









                  w17t

                  2,23441637




                  2,23441637






















                      up vote
                      0
                      down vote













                      Another way :: You can create a batch file and put following code to batch file. Its first run netstat command to get processId for port 9797. And then set it into a variable.



                      @ECHO OFF                                                                              
                      FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
                      SET /A ProcessId=%%T) &GOTO SkipLine
                      :SkipLine
                      echo ProcessId = %ProcessId%
                      PAUSE





                      share|improve this answer























                      • Please explain what this is doing.
                        – Scott
                        Dec 5 at 8:12















                      up vote
                      0
                      down vote













                      Another way :: You can create a batch file and put following code to batch file. Its first run netstat command to get processId for port 9797. And then set it into a variable.



                      @ECHO OFF                                                                              
                      FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
                      SET /A ProcessId=%%T) &GOTO SkipLine
                      :SkipLine
                      echo ProcessId = %ProcessId%
                      PAUSE





                      share|improve this answer























                      • Please explain what this is doing.
                        – Scott
                        Dec 5 at 8:12













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Another way :: You can create a batch file and put following code to batch file. Its first run netstat command to get processId for port 9797. And then set it into a variable.



                      @ECHO OFF                                                                              
                      FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
                      SET /A ProcessId=%%T) &GOTO SkipLine
                      :SkipLine
                      echo ProcessId = %ProcessId%
                      PAUSE





                      share|improve this answer














                      Another way :: You can create a batch file and put following code to batch file. Its first run netstat command to get processId for port 9797. And then set it into a variable.



                      @ECHO OFF                                                                              
                      FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
                      SET /A ProcessId=%%T) &GOTO SkipLine
                      :SkipLine
                      echo ProcessId = %ProcessId%
                      PAUSE






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 5 at 8:57









                      Community

                      1




                      1










                      answered Dec 5 at 7:55









                      flopcoder

                      101




                      101












                      • Please explain what this is doing.
                        – Scott
                        Dec 5 at 8:12


















                      • Please explain what this is doing.
                        – Scott
                        Dec 5 at 8:12
















                      Please explain what this is doing.
                      – Scott
                      Dec 5 at 8:12




                      Please explain what this is doing.
                      – Scott
                      Dec 5 at 8:12


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f597726%2frun-a-process-and-return-its-pid-as-chp-exe-for-a-batch-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                      Mangá

                      Eduardo VII do Reino Unido