1.

Solve : how 2 get PID & kill the process on the basis of PID.......??

Answer»

Dear All,
how to i get PID of a PARTICULAR executable file from multiple running application thru batch file and kill the process on basis of PID? Plsss tell me the Program that what should i write coz i'm a beginner....!!

pls help meWell, that confirms it! Homework!!!

But since I'm in a good MOOD, I'll let you off the hook.how could i get PID from a TASKLIST......?Just stick to one topic, please. Preferably the other one (there are 2 experts active over there, so you should get your answers pretty soon).it was "urgent" before, now he's shut up... what's the betting teacher's flunked him?
Use the following batch file to KILL FIRST TASK...
Code: [Select]@echo off
REM **** KILL_FIRST.BAT ****
set TASKNAME=NOTEPAD.EXE
SETLOCAL ENABLEDELAYEDEXPANSION

echo Task list before KILL_FIRST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

if %TASKTOTAL%==0 goto :eof

set TASKCOUNT=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKCOUNT=!TASKCOUNT!+1
if !TASKCOUNT!==1 taskkill.exe /PID %%B
)

echo Task list after KILL_FIRST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

Example of running KILL FIRST TASK...
Code: [Select]D:\test> NOTEPAD

D:\test> NOTEPAD

D:\test> KILL_FIRST
Task list before KILL_FIRST...
[Task 1 is "notepad.exe 2648"]
[Task 2 is "notepad.exe 4104"]
[Task 2 is last]
SUCCESS: The process with PID 2648 has been terminated.
Task list after KILL_FIRST...
[Task 1 is "notepad.exe 4104"]
[Task 1 is last]

D:\test> KILL_FIRST
Task list before KILL_FIRST...
[Task 1 is "notepad.exe 4104"]
[Task 1 is last]
SUCCESS: The process with PID 4104 has been terminated.
Task list after KILL_FIRST...
INFO: No tasks running with the specified criteria.
[Task 0 is last]

D:\test> KILL_FIRST
Task list before KILL_FIRST...
INFO: No tasks running with the specified criteria.
[Task 0 is last]

D:\test>

Use the following batch file to KILL LAST TASK...
Code: [Select]@echo off
REM **** KILL_LAST.BAT ****
set TASKNAME=NOTEPAD.EXE
SETLOCAL ENABLEDELAYEDEXPANSION

echo Task list before KILL_LAST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

if %TASKTOTAL%==0 goto :eof

set TASKCOUNT=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKCOUNT=!TASKCOUNT!+1
if !TASKCOUNT!==!TASKTOTAL! taskkill.exe /PID %%B
)

echo Task list after KILL_LAST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

Example of running KILL LAST TASK...
Code: [Select]D:\test> NOTEPAD

D:\test> NOTEPAD

D:\test> KILL_LAST
Task list before KILL_LAST...
[Task 1 is "notepad.exe 4276"]
[Task 2 is "notepad.exe 3428"]
[Task 2 is last]
SUCCESS: The process with PID 3428 has been terminated.
Task list after KILL_LAST...
[Task 1 is "notepad.exe 4276"]
[Task 1 is last]

D:\test> KILL_LAST
Task list before KILL_LAST...
[Task 1 is "notepad.exe 4276"]
[Task 1 is last]
SUCCESS: The process with PID 4276 has been terminated.
Task list after KILL_LAST...
INFO: No tasks running with the specified criteria.
[Task 0 is last]

D:\test> KILL_LAST
Task list before KILL_LAST...
INFO: No tasks running with the specified criteria.
[Task 0 is last]

D:\test>

Use the following batch file to KEEP FIRST TASK...
Code: [Select]@echo off
REM **** KEEP_FIRST.BAT ****
set TASKNAME=NOTEPAD.EXE
SETLOCAL ENABLEDELAYEDEXPANSION

echo Task list before KEEP_FIRST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

if %TASKTOTAL%==0 goto :eof
if %TASKTOTAL%==1 goto :eof

set TASKCOUNT=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKCOUNT=!TASKCOUNT!+1
if not !TASKCOUNT!==1 taskkill.exe /PID %%B
)

echo Task list after KEEP_FIRST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

Example of running KEEP FIRST TASK...
Code: [Select]D:\test> NOTEPAD

D:\test> NOTEPAD

D:\test> NOTEPAD

D:\test> KEEP_FIRST
Task list before KEEP_FIRST...
[Task 1 is "notepad.exe 4452"]
[Task 2 is "notepad.exe 5900"]
[Task 3 is "notepad.exe 4496"]
[Task 3 is last]
SUCCESS: The process with PID 5900 has been terminated.
SUCCESS: The process with PID 4496 has been terminated.
Task list after KEEP_FIRST...
[Task 1 is "notepad.exe 4452"]
[Task 1 is last]

D:\test> KEEP_FIRST
Task list before KEEP_FIRST...
[Task 1 is "notepad.exe 4452"]
[Task 1 is last]

D:\test>

Use the following batch file to KEEP LAST TASK...
Code: [Select]@echo off
REM **** KEEP_LAST.BAT ****
set TASKNAME=NOTEPAD.EXE
SETLOCAL ENABLEDELAYEDEXPANSION

echo Task list before KEEP_LAST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

if %TASKTOTAL%==0 goto :eof
if %TASKTOTAL%==1 goto :eof

set TASKCOUNT=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKCOUNT=!TASKCOUNT!+1
if not !TASKCOUNT!==!TASKTOTAL! taskkill.exe /PID %%B
)

echo Task list after KEEP_LAST...
set TASKTOTAL=0
for /F "tokens=1-2" %%A in ('tasklist.exe /nh /fi "imagename eq %TASKNAME%"') do (
set /A TASKTOTAL=!TASKTOTAL!+1
echo [Task !TASKTOTAL! is "%%A %%B"]
)
echo [Task %TASKTOTAL% is last]

Example of running KEEP LAST TASK...
Code: [Select]D:\test> NOTEPAD

D:\test> NOTEPAD

D:\test> NOTEPAD

D:\test> KEEP_LAST
Task list before KEEP_LAST...
[Task 1 is "notepad.exe 5836"]
[Task 2 is "notepad.exe 4776"]
[Task 3 is "notepad.exe 4336"]
[Task 3 is last]
SUCCESS: The process with PID 5836 has been terminated.
SUCCESS: The process with PID 4776 has been terminated.
Task list after KEEP_LAST...
[Task 1 is "notepad.exe 4336"]
[Task 1 is last]

D:\test> KEEP_LAST
Task list before KEEP_LAST...
[Task 1 is "notepad.exe 4336"]
[Task 1 is last]

D:\test>

Hope that helps,
JamesDear All,
well thanx for the KILL.bat for killing process. Now, may i know about another query...!!


In KILL FIRST.bat example in first case if i want to kill 4104 before than 2648 then how would it be POSSIBLE.....?
May i know pls.....?

pls revert back 2 me

Best Regards
AshutoshQuote

In KILL FIRST.bat example in first case if i want to kill 4104 before than 2648 then how would it be possible.....?

Use KILL_LAST.bat ?contrex, do teachers these days give work this hard to students?
The answer seems a little to complicated to be assigned by a teacher.


Discussion

No Comment Found