Search code examples
batch-filepid

Getting PID from running bat script


Hi i'm trying to find a way to get my own PID from a bat script. I found this:

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

that outputs:

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

how would I get the PID number into a variable in my bat script?

any help would be appreciated.


Solution

  • try with getcmdpid , thus you will not need to change the title:

    call getCmdPID.bat
    echo %errorlevel%
    

    to do it with tasklist you'll need for loop to process the output:

    title mycmd
    for /f "tokens=2 delims=," %%a in (
      'tasklist /v /fo csv ^| findstr /i "mycmd"'
    ) do (
      set "mypid=%%~a"
    )
    echo %mypid%
    

    check also this thread: http://www.dostips.com/forum/viewtopic.php?t=6133