当他省略扩展名.exe来杀死进程时如何检查和更正用户输入?

时间:2015-06-13 21:24:31

标签: batch-file cmd

我有这个批处理杀死用户当然键入的一些进程,并且当用户例如键入带有扩展名的 Calc.exe 时它会工作5/5,但我现在的问题是改进此批处理是为了自动添加此程序,如果用户省略添加扩展名 .exe ,如计算而不包含 .exe 将无效

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

所以我专注于这段代码,但没有成功! 这是我的小尝试:

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

那么批量怎么做?

谢谢!

3 个答案:

答案 0 :(得分:3)

如果您的文件从不包含.exe以外的任何其他扩展名,则可以使用"%~n1.exe"获取.exe的文件名。

%~n1只是没有扩展名的文件名。比添加.exe,并在文件名包含空格的情况下在其周围加上引号。

但如果您的可执行文件为example.program.exe,并且用户输入example.program,则会失败。在这种情况下,"%~n1.exe"的结果将为"example.exe"。为避免这种情况,您必须检查扩展名是否为.exe。您可以使用%~x1来获取扩展程序。

有关详细信息,另请参阅Pesticide for chrome

答案 1 :(得分:3)

我想与您分享带有动态菜单的洞脚本,也许这对其他人有帮助,并感谢您的所有贡献;)

(将一些法语单词翻译成英语)

@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

编辑15/06/2015:新版:使用

命令添加搜索文件
    @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

答案 2 :(得分:1)

这样的东西
:loopMain
set /p "process=Entrer le(s) nom(s) de(s) processus> "
if "%process:.exe=%"=="%process%" goto :loopMain

:loopMain
set /p "process=Entrer le(s) nom(s) de(s) processus> "
if /I not "%process:~-4%"==".exe" goto :loopMain

set "process=%process%.exe"代替goto :loopMain

输入多个名称时的类似方法:在简单的for %%a in (%process%) do ...循环中检查正确的用户输入。