batch file - How to check and correct user input when he omit the extension .exe to kill the process? -
i have batch kill process typed of course user , works 5/5 when user example typed calc.exe extension, issue improve batch in order add automatically program if user has omitted add extension .exe calc without extension .exe not 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 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% %%a in (%process%) 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 focusing on piece of code no success ! 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 in batch ?
thank !
if files have never other extension .exe
, can use "%~n1.exe"
filename .exe
.
%~n1
filename without extension. add .exe
, , put quotes around in case filename contains space.
but fail if executable example.program.exe
, , user entered example.program
. in case result of "%~n1.exe"
"example.exe"
. avoid that, have check if extension .exe
. can use %~x1
extension.
see command line arguments (parameters) more information.
Comments
Post a Comment