几十年前我和4DOS一起工作过,最近又搞过一次,但是没有普通Windows批处理经验。我试图制作一些东西来方便地杀死firefox.exe进程,这些进程有时会失火并且永远不会显示Firefox但会持续存在并占用我的资源。
尝试创建默认为终止进程的查询对用户。 第一个问题是"如果%REPLy%== SOMETHING(转到某些地方)"声明。 显式的工作正常但我想杀死firefoxes如果它们都不是真的。我想我只是把代码放在" if ... goto" s但是那不起作用。所以我尝试了另外一个基于变量REPLy等于什么。那不行。所以我想也许一个变量等于什么("")可能与未声明的相同,也许回复的东西只是删除变量而不是给出值""并为此添加了一个if。这也没有用。所以我想也许我不得不把杀戮代码放在一个标签上并在那里发送执行,就像我在那些有用的语句中所做的那样,但那也不起作用。如果我进入SOMETHING而不是NO或no的显式变化,完整或截断,它有效。 taskkill命令报告成功,但实际上仍然失败。但是当我到达那里时,我会在那座桥上工作。当前的问题是如何获取NO条目(换句话说,只需按Enter键)就可以像非字符串一样转到kill代码。我在这里做错了什么?
@echo off
REM All this stuff with the path is because I can't reboot this system right now (long story) and I can not seem to make the amended path stick. So for now, I set it each time. I presume I just need to reboot to make the path setting I changed under computer properties, etc, stick.
echo "This is the path:"
path
PATH=%PATH%;C:\Program Files\GnuWin32\bin
echo "This is the path now:"
path
echo "All these path setting and testing commands and remarks can be cleaned up after I figure out if the new path becomes permanent after reboot."
REM Here ends the stuff I expect to delete after I can reboot.
REM Here begins the part I do not expect to change and that works fine.
tasklist | findstr /B firefox.exe | wc -l > kill_firefox.bat_var.tmp
set /p NUMBER_OF_PROCESSEs=<kill_firefox.bat_var.tmp
del kill_firefox.bat_var.tmp
IF NOT DEFINED NUMBER_OF_PROCESSEs (goto ERROR - NUMBER_OF_PROCESSEs not set)
if %NUMBER_OF_PROCESSEs%==0 (goto NO_PROCESSES)
REM Since the contrary conditions lead to gotos, if processing gets to here, there are 1 or more firefox.exe processes.
echo The number of firefox.exe processes running is:
echo .
echo %NUMBER_OF_PROCESSEs%
echo .
tasklist | findstr /B firefox.exe
set /p REPLy= "Kill these? Y/n"
echo "REPLy is %REPLy%"
pause
if %REPLy%==n (goto USER DECLINED TO KILL)
if %REPLy%==N (goto USER DECLINED TO KILL)
if %REPLy%==no (goto USER DECLINED TO KILL)
if %REPLy%==NO (goto USER DECLINED TO KILL)
REM Here is where the problems start. By my reasoning, I shouldn't need any if/then here, nor even a goto, just the code that is now in the part labeled KILL. The ifs and the goto and putting the code in the labeled section are the result of many attempts to get that code to run with various constructions.
if [%REPLy%] == [] goto KILL
IF NOT DEFINED REPLy (goto KILL)
goto KILL
:NO_PROCESSES
echo There are no firefox.exe processes running.
pause
exit
:ERROR - NUMBER_OF_PROCESSEs not set
echo Logic error - The variable is not defined. This script must be repaired.
pause
exit
:USER DECLINED TO KILL
echo User declined to kill processes.
pause
exit
:KILL
REM I am not sure if ANY of this is running because the pause command is not working and the terminal disappears to fast to see. What am I doing wrong here?
echo killing . . .
taskkill /IM firefox.exe
pause
exit
通过编辑添加: 好吧,我必须把我的聪明药片与我的哑药混淆。这是我如何修复我被困的部分:
我改变了 设置/ p REPLy =&#34;杀了这些? Y / N'#34; 至 设置/ p REPLy =&#34;杀了这些? Y / N'#34; ||设置REPLy = Y. 这就是诀窍。我知道为什么会有效,但我不明白为什么我以前尝试过的方式不合适。显然,批处理变量集(未设置?清除?无效?)由&#34; set / p somemessage&#34;只有一个简单的输入键作为回应,我不明白。但无论如何,我不必理解它,只是接受它。使用&#34; ||&#34;构建以上作品。无论如何,这是我问过的问题的一部分,并且它已经解决了。如果我无法完成剩下的工作,我会在稍微清理一下这批文件后再次发帖。答案 0 :(得分:0)
使用setx
设置永久变量(请参阅setx/?
,语法与set
不同)。
set /p
保持变量不变。因此,您可以预定义变量:
set "REPLy=Y"
set /p "REPLy=Kill these? Y/n"
echo %REPLy%
echo first letter of REPLy is %REPLy:~0,1%
但我会使用set /p
而不是choice
。
没有&#34;空&#34;变量。如果没有值,则不定义变量。
if
有/i
开关忽略大写。
获取我将使用的进程数(不需要外部实用程序):
for /f %%i in ('tasklist ^| find /c "firefox.exe"') do set NUMBER_OF_PROCESSEs=%%i
并且SomethingDark已经提到过:
从命令提示符运行脚本而不是双击它(并使用exit /b
代替exit
)
不要在标签中使用空格。