windows - Fastest way to get the PID of the current running terminal and save to a variable -


i need pid of each open terminal.

i have in works right now. however, doesn't give correct pid, , it's little slow.

@echo off rem note: session name privileged administrative consoles blank. if not defined sessionname set sessionname=console  setlocal set instance=%date% %time% %random% title %instance%  rem pid find /f "usebackq tokens=2" %%a in (`tasklist /fo list /fi "sessionname eq %sessionname%" /fi "username eq %username%" /fi "windowtitle eq %instance%" ^| find /i "pid:"`) set pid=%%a if not defined pid /f "usebackq tokens=2" %%a in (`tasklist /fo list /fi "sessionname eq %sessionname%" /fi "username eq %username%" /fi "windowtitle eq administrator:  %instance%" ^| find /i "pid:"`) set pid=%%a if not defined pid echo !error: not pid of current process.  exiting.& exit /b 1  echo here's pid: %pid% 

i'm not sure how explain it, whenever runs, doesn't give pid of cmd.exe, returns pid of overall process itself. if ran normal cmd, return right pid, if ran different terminal cmder, give pid of cmder, not affiliated cmd.exe.

i've seen lot of solutions use for loop wmic, can't seem work. every time try it, pid returns different each time run it, wrong.

i can use following, , works 3rd party terminals , faster have above:

wmic process "name='wmic.exe'" parentprocessid 

i'm not sure how can extract pid out of output of this. there quicker way of getting pid of terminal?

using idea in comment:

wmic /output:temp.txt process "caption='wmic.exe'" parentprocessid /f "skip=1" %%a in ('type temp.txt') if not defined pid set "pid=%%a" del temp.txt echo=%pid% 

you can adjust wmic command, use where "commandline like…if feel there's possibility of wmic process running. add small time increase though.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -