当他省略扩展名 .exe 以终止进程时,如何检查和更正用户输入? [英] How to check and correct user input when he omit the extension .exe to kill the process?

查看:19
本文介绍了当他省略扩展名 .exe 以终止进程时,如何检查和更正用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个批处理来终止用户当然键入的某些进程,并且当用户例如使用扩展名键入 Calc.exe 时,它可以正常工作 5/5,但我现在的问题是改进如果用户省略了添加扩展名 .exe 像没有扩展名 .exeCalc 这样的程序将自动添加此批处理将不起作用.

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

那么如何批量处理?

谢谢!

推荐答案

如果你的文件除了 .exe 之外没有任何其他扩展名,你可以使用 "%~n1.exe" 使用 .exe 获取文件名.

If your files have never any other extension than .exe, you can use "%~n1.exe" to get the filename with .exe.

%~n1 只是没有扩展名的文件名.然后添加 .exe,并在其周围加上引号,以防文件名包含空格.

%~n1 is just the filename without extension. Than add .exe, and put quotes around it in case the filename contains a space.

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

But this will fail if your executable is example.program.exe, and the user entered example.program. In this case the result of "%~n1.exe" will be "example.exe". To avoid that, you have to check if the extension is .exe. You can use %~x1 to get just the extension.

另请参阅命令行参数(参数)了解更多信息.

See also Command Line arguments (Parameters) for more information.

这篇关于当他省略扩展名 .exe 以终止进程时,如何检查和更正用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆