Figure Out the batch you want in Windows Task Manager may be a challenging task. In this tutorial, we see how to find the script efficiently and how to change batch process name to a unique one.
Dan have asked me, after reading Change python process name
I have a similar problem with batch files. It is so frustrating.
I am running several batch files. How can I change the process name of a batch file? When you open the Windows Task Manager, I only cmd.exe process. Therefore, I believe that the cmd.exe command runs the batch files.
Can you adjust your script to allow me to find the batch script I want?
First, Dan was right. Whenever you execute a batch script, Windows create a new cmd.exe that runs the script. Therefore, Dan sees all those cmd.exe processes in the Windows task manager. One for each script that is currently run.
Let’s see if the solution we described in the above article can also work here.
Figure Out Batch Script You Want In Windows Task Manager
As Dan describes in his question, when you invoke a batch file demo.bat
, the system will invoke cmd.exe
with the following command cmd.exe /C demo.bat
. Therefore, when you invoke several batch script or invoke the same batch script several times, you will several cmd.exe
processes in the Windows task manager.
After some time, you may want to kill one of the batch scripts you run. However, it is a challenging task – In Windows Task Manager, you do not see the names of the batch files but many cmd.exe
processes. Which one is the batch script you want to kill? How to figure out which batch script is which? You do not have a clue.
Solution To Figure Out The batch You Want
As said, The cmd.exe is the process that executes batch files. Since Windows Task Manager displays the image name of the process, It displays cmd.exe for each invoked batch script regardless of the batch file name.
If we could change batch process name and modify the image name of the cmd.exe
to a unique name for each batch script file you want to run, we would quickly and easily figure out which script process you want in the Windows Task Manager. Let’s describe this solution:
Copy the cmd.exe
to a unique name (For example, my-script.exe) for each batch script you want to invoke. Next, invoke the my-script.exe /C batch_file.bat
instead of batch_file.bat
. Now when we open Windows Task Manager, we can quickly figure out which batch script is which since we will see several processes of different binary files instead of several processes of cmd.exe
Script To Change Batch Process Name
The following batch script automates the above solution. Create a new file named batch-by-name.bat
in the windows directory with the following contents:
batch-by-name.bat
echo off
set alias_name=%1
set batch_file=%2
set alias_path=%~dp0
set program=c:\Windows\system32\cmd.exe
set alias=%alias_path%%alias_name%.exe
call :find_args %*
call :make_link %program% %alias%
%alias% /C %batch_file% %args%
goto :eof
:find_args
set args=
shift
shift
:loop
if [%1] == [] goto :eof
set args=%args% %1
shift
goto :loop
:make_link
copy %1 %2
goto :eof
What the script do?
set alias_name=%1
set batch_file=%2
set alias_path=%~dp0
set program=c:\Windows\system32\cmd.exe
set alias=%alias_path%%alias_name%.exe
We store in batch_file
, the value of the second argument to the batch-by-name.bat
script. It should be the path to the script we want to run.
We store in program is the full path to the cmd.exe
.
We store in alias the full path to the copied cmd.exe
. Thus, the name of the copied cmd.exe
is the first argument to the batch-by-name.bat
script. The directory of the copied cmd.exe
, is the directory where the batch-by-name.bat
script was invoked.
call :find_args %*
call :make_link %program% %alias%
%alias% /C %batch_file% %args%
The :find_args
get all the batch script arguments from third position to args variable. Then, we copy the cmd.exe
to the new name and finally we invoke it the way we invoke cmd.exe to a batch file.
How To Use?
Now, Let’s see how we can use this above batch to distinguish between your python scripts.
Suppose you want to run the following command:
Command A – Invoke the batch file
my.bat arg1 arg2 arg3 arg4
Invoke the following command instead:
Command B – Invoke with batch-by-name
batch-by-name my-script my.bat arg1 arg2 arg3 arg4
The above commands : command A (the batch script) and command B (batch-by-name), will run the same script with the same arguments. However, when we open Windows Task Manager, we see that the process name of command A is cmd.exe while the process name of command A is my-script.exe.
If we change the unique name in command B for each batch script we invoke, we can quickly and easily figure out the batch you want in the Windows Task Manager.