How to check and correct user input ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 63
Joined: 15 Apr 2014 17:59

How to check and correct user input ?

#1 Post by Hackoo » 13 Jun 2015 16:02

Hi all :wink:
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.

Code: Select all

  @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 :

Code: Select all

: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 !

Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: How to check and correct user input ?

#2 Post by Ben Mar » 13 Jun 2015 17:02

Just change this:

Code: Select all

:KillProcess
    Taskkill /IM "%~1*" /F >> %TmpFile% 2>&1

Hackoo
Posts: 63
Joined: 15 Apr 2014 17:59

Re: How to check and correct user input ?

#3 Post by Hackoo » 13 Jun 2015 17:15

Ben Mar wrote:Just change this:

Code: Select all

:KillProcess
    Taskkill /IM "%~1*" /F >> %TmpFile% 2>&1

Thank you !
it's a good catch and it is working like a charm :wink: :lol:
And i found another solution on StackOverflow by Wimmel :
You can use %~x1 to get the extension. If your files have never any other extension than .exe, you can use "%~n1.exe" to get the filename with .exe

Code: Select all

:KillProcess
Taskkill /IM "%~n1.exe" /F >> %TmpFile% 2>&1

Post Reply