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

John Savill

April 27, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

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
:next
.......
:SETPID
set MASTER_PID=%1
goto :next
.......

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
:next
.......
:SETPID
set MASTER_PID=%1
goto :next
.......

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

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like