How to automatically find PID of opened program?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lokino
Posts: 1
Joined: 11 Oct 2012 09:41

How to automatically find PID of opened program?

#1 Post by lokino » 22 Oct 2012 12:29

I don't mean how to find the PID, I'm aware of how(the graphical method at least, I've never experimented with using CMD to do it). I'm wondering if there's a way to open a program and then automatically find it's PID without user input.(ie: looking for the code myself) I operate mainly off of a flash drive and public computers, so I've made a batch script to streamline my first usage. (installation, setting tweaks, etc.) Right now it looks like this:

Code: Select all

@ECHO off
net stop beep
Echo Beep has been bopped.
PING 127.0.0.1 -n 2 >null
Echo Opening AHK Installer, press any key once completed.
start "" "G:\Installers\AutoHotkey104805_Install.exe"
pause >null
echo Raising sounds.
start "" "G:\startup\sound.exe"
ping 127.0.0.1 -n 2 >null
echo Opening WinRAR installer, press any key once completed.
start "" "G:\Installers\WinRAR v3.62 Corp.exe"
pause >null
echo Installing cmdow
start "" "G:\Installers\cmdow.exe"
ping 127.0.0.1 -n 2 >null
echo Launching Vidalia, press any key when connected to TOR.
start "" "G:\Tor Browser\App\vidalia.exe"
pause >null
start
echo.
echo Continuing to Program Management.
ping 127.0.0.1 -n 2 >null
echo.

Ignoring my obsessive use of ping, you'll see that I've a lot of installers being opened which I then have to finish before tapping a key to continue. I'd like to use wait to streamline the process, but since the PID changes every time I open it, I can't figure out a way to use it. Any ideas?

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: How to automatically find PID of opened program?

#2 Post by Boombox » 22 Oct 2012 13:11

At CMD:

TASKLIST
Will show you what's running, giving PID and such...

TASKKILL
Will KILL a task based on your input...

Code: Select all

taskkill /f /fi "PID eq 5588"


The above code, terminates the process with PID EQual to 5588

/F = Force task to close
/FI = The filter used to identify the task. (PID in this instance)

But if you know what you want to close, use...

Code: Select all

taskkill /f /fi "imagename eq AutoHotkey104805_install.exe"


/FI = 'Imagename' is the filter used here...

----------------

Type Tasklist /? or Taskkill /? at the CMD Prompt for tips...

----------------


Code: Select all

@echo off
start calc.exe
timeout /t 5 >nul
for /f "tokens=2" %%g in ('tasklist ^| findstr calc.exe') do taskkill /f /fi "PID eq %%g"
pause>nul

Post Reply