Skip to main content
goto not required
Source Link
Karan
  • 56.6k
  • 20
  • 120
  • 194

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
    goto :EOF
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
    goto :EOF
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)
added 1087 characters in body
Source Link
Karan
  • 56.6k
  • 20
  • 120
  • 194

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
    goto :EOF
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
    goto :EOF
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)
Source Link
Karan
  • 56.6k
  • 20
  • 120
  • 194

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.