从运行bat脚本获取PID

时间:2017-06-02 13:51:05

标签: batch-file pid

您好我正试图找到一种从蝙蝠脚本中获取自己的PID的方法。 我发现了这个:

title=mycmd
tasklist /v /fo csv | findstr /i "mycmd"

输出:

"cmd.exe","15084","RDP-Tcp#5","2","2,768 K","Running","MEDIASERVER\Administrator
","0:00:00","Administrator: =mycmd"

如何在我的bat脚本中将PID编号转换为变量?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Prepare a temporary file reference where to send the wmic output
    for %%t in ("%temp%\%~nx0.%random%%random%%random%%random%%random%.tmp") do > "%%~ft" (

        rem Call wmic to retrieve its own parent process ID, that is, our cmd instance
        wmic process where "name='wmic.exe' and commandline like '%%_%random%%random%%random%_%%'" get parentprocessid

        rem Read the PID from the generated temporary file
        for /f "skip=1" %%a in ('type "%%~ft"') do set "processID=%%a"

        rem And remove the temporary file
    ) & 2>nul del /q "%%~ft"

    echo %processID%

答案 1 :(得分:3)

尝试使用getcmdpid,因此您无需更改标题:

<?php
     if(is_array($items) && count($items) > 0){//check that we have something before proceeding with the loop
       foreach($items as $row){
        $partno=$row['partno'];
        $desc=$row['description'];
        $qty=$row['quantity'];
      ?>
      <tr>
      <td><?=$partno?></td>
      <td><?=$desc?></td>
      <td><?=$qty?></td>
      </tr>

      <?php  
        }
      } else {
          ?>
      <h3 style="color: red;">Item not found!</h3>
      <?php
        }
      ?>      

使用您需要循环处理输出的任务列表来执行此操作:

call getCmdPID.bat
echo %errorlevel%

还检查这个帖子: http://www.dostips.com/forum/viewtopic.php?t=6133