How do I obtain the currently running batch file’s PID?

A. If you’ve installed the Microsoft Windows NT Resource Kit, you can use the following batch file to obtain the currently running batch file’s process identifier (PID). To use this script at the command line, change the double percentage sign to a single character (i.e., %).

for /f "Tokens=*" %%I in ('f:\tlist ^| grep %0 ^| grep CMD ^| awk "\{ print $1 \}"') do call :SETPID %%I<br>
:next<br>
.......<br>
:SETPID<br>
set MASTER_PID=%1<br>
goto :next<br>
.......

The following alternative uses the resource kit’s Tlist utility.

for /f "Tokens=1 Delims= " %I in ('c:\tlist ^| find "%0"') do goto :SETPID %I<br>
:next<br>
.......<br>
:SETPID<br>
set MASTER_PID=%1<br>
goto :next<br>
.......

Use the command tlist rather than pulist, because pulist doesn’t give enough information. You need the batch file’s name, which tlist provides. Because the shell uses the pipe character (i.e., |), you must use the caret character (i.e., ^) to escape the pipe.

You can compile the tlist alternative on one line, as follows.

for /f "Tokens=1 Delims= " %I in ('c:\tlist ^| find "%0"') do set MASTER_PID=%1
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish