How to check and fix user input when he omits the .exe extension to kill the process?
I have this batch to kill some process which is of course the user and it runs 5/5 when the user for example typed Calc.exe with the extension, but now my problem is to improve this package to add this program automatically unless user added .exe extension like Calc without .exe extension won't work.
@Echo off & cls
Mode con cols=72 lines=7
Set TmpFile=TmpFile.txt
Set Resultat=KillResult.txt
If Exist %TmpFile% Del %TmpFile%
If Exist %Resultat% Del %Resultat%
::********************************************************************************************
:Main
Title Process Killer by Hackoo 2015
cls & color 0B
echo.
echo Quel(s) processus voulez-vous fermer ?
echo.
set/p "process=Entrer le(s) nom(s) de(s) processus> "
cls & color 0C
Title Killing "%process%" ...
echo.
echo Killing "%process%" ...
echo.
echo %date% *** %time% >> %TmpFile%
For %%a in (%process%) Do Call :KillProcess %%a
Cmd /U /C Type %TmpFile% > %Resultat%
Start %Resultat%
echo.
Goto :Main
::*********************************************************************************************
:KillProcess
Taskkill /IM "%~1" /F >> %TmpFile% 2>&1
echo ***************************************************************************** >> %TmpFile%
::*********************************************************************************************
So, I'm focused on this piece of code, but I'm running out of time! this is my little try:
:KillProcess
Set str=%~1
set str=%str:~-4%
echo.%str%
pause
if %str%==".exe" (Taskkill /IM "%~1" /F >> %TmpFile% 2>&1) || (Taskkill /IM "%~1.exe" /F >> %TmpFile% 2>&1)
So how do you do it in batch?
Thank!
If your files never have any extension other than .exe
, you can use "%~n1.exe"
to get the filename from .exe
.
%~n1
is just a filename without an extension. Then add .exe
and put quotes around it if the filename contains a space.
But it will fail if your executable example.program.exe
and the user entered example.program
. In this case, the result "%~n1.exe"
will be "example.exe"
. To avoid this, you must check if the extension is .exe
. You can use %~x1
to get just the extension.
See also Command Line Arguments (Parameters) for more information.
I just want to share with you a script hole with a dynamic menu, maybe it would be helpful for other people and thank you for all your contributions;)
(Translation of some French words into English)
@Echo off & cls & color 0B
Mode con cols=72 lines=10
Set TmpFile=TmpFile.txt
Set Resultat=KillResult.txt
If Exist %TmpFile% Del %TmpFile%
If Exist %Resultat% Del %Resultat%
:menuLOOP
Cls & color 0B
Title Process Starter and Killer by Hackoo 2015
echo.
echo. ==========================Menu============================
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo. %%B %%C
echo.
echo. ==========================================================
set choice=
echo. & set /p choice=Make a choice or hit ENTER to quit: || GOTO :EOF
echo. & call :menu_[%choice%]
GOTO:menuLOOP
::********************************************************************************************
:menu_[1] Start a Process
cls & color 0B
echo.
Set /p "MyProcess=Enter the process name to start it> "
echo.
echo Le processus %MyProcess% est lance ...
Start %MyProcess%
GOTO :menuLOOP
::********************************************************************************************
:menu_[2] Kill a Process or Multi Processes
Title Process Killer by Hackoo 2015
cls & color 0B
echo.
echo What process do you want to kill ?
echo.
set/p "process=Enter the process name or processes names separated by a space> "
cls & color 0C
Title Killing "%process%" ...
echo.
echo Killing "%process%" ...
echo.
echo %date% *** %time% >> %TmpFile%
For %%a in (%process%) Do Call :KillMyProcess %%a
Cmd /U /C Type %TmpFile% > %Resultat%
Start %Resultat%
GOTO :menuLOOP
::*********************************************************************************************
:KillMyProcess
Taskkill /IM "%~n1.exe" /F >> %TmpFile% 2>&1
echo ***************************************************************************** >> %TmpFile%
exit /b
::*********************************************************************************************
:EOF
EXIT
EDIT 06/15/2015: New version : adding Find files using command Where
@Echo off & cls & color 0B
Mode con cols=72 lines=11
Set TmpFile=TmpFile.txt
Set Resultat=KillResult.txt
If Exist %TmpFile% Del %TmpFile%
If Exist %Resultat% Del %Resultat%
:menuLOOP
Mode con cols=72 lines=11
Cls & color 0B
Title Process Starter and Killer by Hackoo 2015
echo(
echo( ==========================Menu============================
echo(
::Suggestion de Walid : optimisation au niveau du parseur : utiliser deux tokens au lieu de trois
for /f "tokens=2* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo( %%A %%B
::for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo( %%B %%C
echo(
echo( ==========================================================
set choice=
echo( & set /p choice=Make a choice or hit ENTER to quit: || GOTO :EOF
echo( & call :menu_[%choice%]
GOTO:menuLOOP
::********************************************************************************************
:menu_[1] Start a Process
cls & color 0B
echo(
Set /p "MyProcess=Enter the process name to start it> "
echo(
echo Le processus %MyProcess% est lance ...
Start %MyProcess%
GOTO :menuLOOP
::********************************************************************************************
:menu_[2] Kill a Process or Multi Processes
Title Process Killer by Hackoo 2015
cls & color 0B
echo(
echo What process(es) do you want to kill ?
echo(
set /p "process=Enter the process name or processes names separated by a space> "
cls & color 0C
Title Killing "%process%" ...
echo(
echo Killing "%process%" ...
echo(
echo %date% *** %time% >> %TmpFile%
For %%a in (%process%) Do Call :KillMyProcess %%a
Cmd /U /C Type %TmpFile% > %Resultat%
Start %Resultat%
GOTO :menuLOOP
::********************************************************************************************
:menu_[3] Search for files
cls & color 0A
echo(
Set /p "Location=Enter the folder location where do you want to serach> "
cls & echo(
echo You chose this location "%Location%"
echo(
Set /p "FileName=Enter the file name to looking for> "
Mode con cols=90 lines=11
Set Tmp=Tmp.txt
Set SearchResult=SearchResult.txt
echo( & cls
echo( & echo Please Wait for moment .... Searching for "%FileName%" on "%Location%"
where /r "%Location%" "%FileName%" > %Tmp%
::Pour lire la liste en Unicode (Arabe)
Cmd /U /C Type %Tmp% > %SearchResult%
Del %Tmp%
Start %SearchResult%
GOTO :menuLOOP
::*********************************************************************************************
:KillMyProcess
Taskkill /IM "%~n1.exe" /F >> %TmpFile% 2>&1
echo ***************************************************************************** >> %TmpFile%
exit /b
::*********************************************************************************************
:EOF
EXIT
Something like
:loopMain
set /p "process=Entrer le(s) nom(s) de(s) processus> "
if "%process:.exe=%"=="%process%" goto :loopMain
or
:loopMain
set /p "process=Entrer le(s) nom(s) de(s) processus> "
if /I not "%process:~-4%"==".exe" goto :loopMain
or set "process=%process%.exe"
instead of goto :loopMain
.
A similar approach in case of entering multiple names: check the correct user input in a simple loop for %%a in (%process%) do ...
.