How to get the pid of a windows process right after the process is run

Asked

Viewed 1,000 times

3

How to get the pid of a windows process right after the process is executed, similar to the command $! that retrieves the pid from the process that was executed needed in a way to grab that pid from the windows command to apply to a batch script. some dirt on how to proceed?

2 answers

4


It is possible to do a search on the return of the utilities tasklist and wmic with the command for, in both it is necessary to pass the name of the process.

In the examples below I set the app variable with the name of the app I want to know the PID.

Example with tasklist:

set app=notepad.exe

start %app%

for /F "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq %app%"') do set MyPID=%%b
echo %MyPID%

pause

Example with wmic:

set app=notepad.exe

start %app%

for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%app%'" get ProcessID ^| findstr [0-9]') do set MyPID=%%a
echo  %MyPID%

pause

Original response

  • thanks, I’ll be testing this code for weeks.

1

inserir a descrição da imagem aqui

  • Understand that we don’t have a command available / specific to get that information in the system Windows, the option was to make a bat to obtain that information ...

Perform a process/programme and right after run this bat to get the Process ID

* Observações necessárias:

To obtain the PID (process ID) via a bat, we’ll use some commands who will be visible in execution of own bat, and employ some filters in these commands used to obtain the last PID, preventing it from being the own control used identified as latest IPD

As you can see in the code below, the commands used are being filtered in listings/executions, avoiding, for example, that TaskList be the last process initiated, and identified erroneously in the execution by bat looking for the last executed process...

Get_Last_PID.cmd

@echo off && setlocal enabledelayedexpansion & title <nul
    
rem :: arquivos excluidos da busca/listagem de tarefas e serão usado nesse script ::  
set "_dt_criacao=wmic process get name^,creationdate"
set "_ignoras_01=findstr /vli "tasklist. findstr. cmd.""
set "_ignoras_02=findstr /vli "WMIC. WmiPrvSE.""
    
:: contagem de processos e setando para pular 90% (aproximadamente) no looping for
for /f "tokens=1delims=:" %%c in ('wmic process get name^,creationdate^|find /V /C "^^"')do set "_cnt=%%c"
set /a _skip=(!_cnt! * 90) / 100
    
for /f "skip=%_skip%tokens=1,2delims=%_tab%, " %%i in ('!_dt_criacao!^|!_ignoras_01!^|!_ignoras_02!')do (
    if /i "./" equ "./%%j" goto :_continue_: 
    set "_time_id=%%i" & set "_time_=!_time_id:~8,-4!" & set "_name=%%j"
    set "_time_=!_time_:~,13!" & set "_time_=!_time_:~0,2!:!_time_:~2,2!:!_time_:~4,5!"
    for /f "tokens=1,2delims= " %%P in ('tasklist^|findstr /i "!_name!"')do set "_name_=%%P" & set "_pid=%%Q"
    ) 2>nul 
:_continue_:
echo/ Ultimo Processo Iniciado !_name_! hora !_time_! PID !_pid!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.