You are not logged in.

#1 13 Jun 2015 23:01

Hackoo
Member
Registered: 05 Feb 2015
Posts: 21

How to check and correct user input when he omit the extension .exe ?

Hi all  smile
I have this batch to kill some process typed of course by the user and it works 5/5 when the user for example typed Calc.exe with the extension, but my issue now is to improve this batch in order to add automatically with this program if the user has omitted to add extension .exe like Calc without extension .exe will 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 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 focusing in this piece of code but no success !
this 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 to do that in batch ?
Thank you !

Offline

#2 15 Jun 2015 13:13

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: How to check and correct user input when he omit the extension .exe ?

:KillProcess
Set "ext=%~x1"
echo "%ext%"
pause

Read the last page of for /? help (in English Windows anyway) for the modifiers.

Offline

#3 15 Jun 2015 14:09

Hackoo
Member
Registered: 05 Feb 2015
Posts: 21

Re: How to check and correct user input when he omit the extension .exe ?

Thank you foxidrive to pay attention to my request wink
So i solved my issue in stackoverflow and i shared the main script there, and you can take a look on it; and if there is something to improve please don't hesitate to add it

Offline

Board footer

Powered by