- 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!
thanks, I’ll be testing this code for weeks.
– Márcio César