|
gromancer
初级用户
 
积分 20
发帖 7
注册 2007-2-17
状态 离线
|
『楼 主』:
如何检测特定进程
使用 LLM 解释/回答一下
我想实现这样一个功能:用tasklist列出当前进程清单,然后用find来搜索进程中是否含有1.exe,2.exe,3.exe......(假设啊)如果清单中含有其中一个就执行某个操作。我想用FOR来实现循环可是怎么试也不行啊!!我是这样试的:
tasklist | for %%j in (1.exe,2.exe,3.exe) do find "%%j" && goto :p
find是不是不能用for啊?请高人指点。
Last edited by namejm on 2007-2-20 at 02:56 PM ]
I want to implement such a function: Use tasklist to list the current process list, then use find to search whether the process contains 1.exe, 2.exe, 3.exe... (hypothetically). If the list contains one of them, perform a certain operation. I want to use FOR to implement the loop but it doesn't work no matter how I try! This is how I tried:
tasklist | for %%j in (1.exe,2.exe,3.exe) do find "%%j" && goto :p
Is find not usable with for? Please help from the experts.
Last edited by namejm on 2007-2-20 at 02:56 PM ]
|
|
2007-2-17 03:18 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
tasklist | findstr "1.exe 2.exe 3.exe"&&goto :p
for不会接受传递自tasklist的数据,它是打in来定义的
tasklist | findstr "1.exe 2.exe 3.exe"&&goto :p
The for loop does not accept data passed from tasklist; it is defined with in
|
|
2007-2-17 03:28 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
@echo off
set 1.exe=被定义的程序
set 2.exe=被定义的程序
set 3.exe=被定义的程序
for /f "tokens=1" %%i in ('tasklist /NH') do (
if defined %%i echo %%i
)
pause>nul
@echo off
set 1.exe=Program defined
set 2.exe=Program defined
set 3.exe=Program defined
for /f "tokens=1" %%i in ('tasklist /NH') do (
if defined %%i echo %%i
)
pause>nul
|
|
2007-2-17 03:31 |
|
|
gromancer
初级用户
 
积分 20
发帖 7
注册 2007-2-17
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
挖,好快啊!
谢2楼滴!
3楼slore,你的例子中'tasklist /NH'的/NH是什么意思啊?还有
if defined %%i echo %%i 这个我还不太懂。
pause>nul 又是什么意思呢? 我菜,嘿嘿,麻烦再具体点,谢谢。
Wow, that's so fast!
Thanks to the person on the 2nd floor!
For the 3rd floor slore, what does /NH in 'tasklist /NH' mean in your example? Also, I don't quite understand 'if defined %%i echo %%i'.
And what does 'pause>nul' mean? I'm a noob, heh. Please be more specific, thanks.
|
|
2007-2-17 03:42 |
|
|
gromancer
初级用户
 
积分 20
发帖 7
注册 2007-2-17
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
我发现这个论坛气氛真是好,找到这样一个好的学习地方真不容易!请各位多多指点,嘿嘿!
I find that the atmosphere of this forum is really good. It's really not easy to find such a good place for learning! Please give more guidance to everyone, heh heh!
|
|
2007-2-17 03:46 |
|
|
qasa
高级用户
   
积分 959
发帖 311
注册 2006-4-11 来自 广东-LianJiang
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by gromancer at 2007-2-17 03:42:
挖,好快啊!
谢2楼滴!
3楼slore,你的例子中'tasklist /NH'的/NH是什么意思啊?还有
if defined %%i echo %%i 这个我还不太懂。
pause>nul 又是什么意思呢? 我菜,嘿嘿,麻烦再具体点,谢谢。
TASKLIST /NH
指定栏标头不应该在输出中显示。
只对 "TABLE" 和 "CSV" 格式有效。
if defined ....
如果已定义环境变量,DEFINED 条件的作用是它取得一个环境变量
pause >nul
把显示结果屏蔽掉了,不在屏幕上显示
Originally posted by gromancer at 2007-2-17 03:42:
Wow, so fast!
Thanks to the second floor!
Third floor slore, what does /NH in your example 'tasklist /NH' mean? Also, I don't quite understand "if defined %%i echo %%i".
What does "pause>nul" mean? I'm a newbie, heh. Please be more specific, thanks.
TASKLIST /NH
Specifies that column headers should not be displayed in the output.
Only valid for "TABLE" and "CSV" formats.
if defined ....
The DEFINED condition, if an environment variable is defined, it gets an environment variable
pause >nul
Masks the display result and does not display it on the screen
|

κχυμγνξοθπρωψιαδλεηφβτζσ┬╀┾┳┞┯┰┱┣┲┳╂╁│├┟┭┠这是什么??这就是我的人生 |
|
2007-2-17 05:14 |
|
|
longmarchw
初级用户
 
积分 21
发帖 10
注册 2007-11-20
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
3楼代码能找到指定进程吗?写错了吧。
Can the code on floor 3 find the specified process? You must have made a mistake.
|
|
2010-3-22 21:15 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by longmarchw at 2010-3-22 21:15:
3楼代码能找到指定进程吗?写错了吧。
三楼滴代码没任何问题
三楼滴是传说中的牛人
@echo off
set qq.exe=腾迅QQ&set explorer.exe=外壳&set iexplore.exe=浏览器&for /f %%i in ('tasklist /NH') do if defined %%i call echo %%%%i%%%%i
pause>nul
Last edited by Hanyeguxing on 2010-3-22 at 21:37 ]
Originally posted by longmarchw at 2010-3-22 21:15:
Can the code on floor 3 find the specified process? It's written wrong, right?
The code on floor 3 is completely fine.
The person on floor 3 is a legendary expert.
@echo off
set qq.exe=腾讯QQ&set explorer.exe=外壳&set iexplore.exe=浏览器&for /f %%i in ('tasklist /NH') do if defined %%i call echo %%%%i%%%%i
pause>nul
Last edited by Hanyeguxing on 2010-3-22 at 21:37 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-3-22 21:27 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by longmarchw at 2010-3-22 21:15:
3楼代码能找到指定进程吗?写错了吧。
三楼滴代码没任何问题
三楼滴是传说中的牛人
@echo off
set qq.exe=腾迅QQ&set explorer.exe=外壳&set iexplore.exe=浏览器&for /f %%i in ('tasklist /NH') do if defined %%i call echo %%%%i%%%%i
pause>nul
Last edited by Hanyeguxing on 2010-3-22 at 21:36 ]
Originally posted by longmarchw at 2010-3-22 21:15:
Can the code on floor 3 find the specified process? It's wrong, right?
The code on floor 3 is perfectly fine.
The person on floor 3 is a legendary expert.
@echo off
set qq.exe=腾讯QQ&set explorer.exe=外壳&set iexplore.exe=浏览器&for /f %%i in ('tasklist /NH') do if defined %%i call echo %%%%i%%%%i
pause>nul
Last edited by Hanyeguxing on 2010-3-22 at 21:36 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-3-22 21:28 |
|
|
longmarchw
初级用户
 
积分 21
发帖 10
注册 2007-11-20
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
是我误解了!不好意思啊~~
我误以为 set 1.exe=被定义的程序中 1.exe 就是 1.exe,其实应该是将被Kill的进程。而“被定义的程序”,则随便写了……
恩 ~向大牛学习,谢谢楼上指点!
I misunderstood! Sorry~~
I mistakenly thought that set 1.exe = the defined program where 1.exe is 1.exe. Actually, it should be the process to be killed. And "the defined program" is just written casually...
Hmm~Learn from the experts, thank you for the guidance from the above!
|
|
2010-3-22 21:35 |
|
|
longmarchw
初级用户
 
积分 21
发帖 10
注册 2007-11-20
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
还有个问题:
@echo off
set notepad.exe=notepad.exe
set mspaint.exe=mspaint.exe
set calc.exe=calc.exe
set num=0
setlocal EnableDelayedExpansion
for /f "tokens=1" %%i in ('tasklist /NH') do (
if defined %%i (
echo kill... %%i
taskkill /f /t /im %%i
set/A num=!num!+1
)
)
echo %num% processes were terminated.
pause>nul
如果 taskkill /f /t /im %%i 语句写错了。 num还会累加,怎么处理不让它累加呢?
There is a problem here:
@echo off
set notepad.exe=notepad.exe
set mspaint.exe=mspaint.exe
set calc.exe=calc.exe
set num=0
setlocal EnableDelayedExpansion
for /f "tokens=1" %%i in ('tasklist /NH') do (
if defined %%i (
echo kill... %%i
taskkill /f /t /im %%i
set/A num=!num!+1
)
)
echo %num% processes were terminated.
pause>nul
If the statement taskkill /f /t /im %%i is written incorrectly. Will num still accumulate, how to handle it so that it does not accumulate?
|
|
2010-3-22 21:40 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
你运行我写的代码测试下看,就知道set qq.exe=腾迅QQ可不一定是随便写写的
call echo %%%%i%%%%i中:
对于set来说,%%%%i%%显示的是set时的值,%%i显示的是set时的名。
对于for来说,%%i显示的是变量的值,%%%%i%%显示的是以%%i变量值为名的变量的值。
在开启变量延迟时,call echo %%%%i%%%%i写成echo !%%i!%%i,这样就更明显了
运行后如下返回:
外壳explorer.exe
腾迅QQQQ.exe
浏览器iexplore.exe
浏览器iexplore.exe
浏览器iexplore.exe
Last edited by Hanyeguxing on 2010-3-22 at 21:51 ]
Run the code I wrote to test, and you'll know that set qq.exe=腾讯QQ isn't just written casually.
In call echo %%%%i%%%%i:
For set, %%%%i%% shows the value when set, and %%i shows the name when set.
For for, %%i shows the value of the variable, and %%%%i%% shows the value of the variable named by the value of %%i.
When variable delay is enabled, call echo %%%%i%%%%i is written as echo !%%i!%%i, which is more obvious.
The running results are as follows:
Shell explorer.exe
Tencent QQQQ.exe
Browser iexplore.exe
Browser iexplore.exe
Browser iexplore.exe
Last edited by Hanyeguxing on 2010-3-22 at 21:51 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-3-22 21:43 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Originally posted by longmarchw at 2010-3-22 21:40:
@echo off
set notepad.exe=notepad.exe
set mspaint.exe=mspaint.exe
set calc.exe=calc.exe
set num=0
setlocal EnableDelayedExpansion
for /f "tokens=1" %%i in ('tasklist /NH') do (
if defined %%i (
echo kill... %%i
taskkill /f /t /im %%i
set/A num=!num!+1
)
)
echo %num% processes were terminated.
pause>nul
如果 taskkill /f /t /im %%i 语句写错了。 num还会累加,怎么处理不让它累加呢?
任何批处理都是按他写对了来算的。。。
错误的结果也不一样,不一定会累加,甚至都可能不会运行
可以使用if来检测错误%ERRORLEVEL%
Last edited by Hanyeguxing on 2010-3-22 at 21:53 ]
Originally posted by longmarchw at 2010-3-22 21:40:
@echo off
set notepad.exe=notepad.exe
set mspaint.exe=mspaint.exe
set calc.exe=calc.exe
set num=0
setlocal EnableDelayedExpansion
for /f "tokens=1" %%i in ('tasklist /NH') do (
if defined %%i (
echo kill... %%i
taskkill /f /t /im %%i
set/A num=!num!+1
)
)
echo %num% processes were terminated.
pause>nul
If the taskkill /f /t /im %%i statement is wrong. The num will still accumulate. How to handle it so that it doesn't accumulate?
Any batch processing is calculated based on it being correct...
The wrong result is also different, not necessarily accumulating, and may not even run.
You can use if to detect the error %ERRORLEVEL%
Last edited by Hanyeguxing on 2010-3-22 at 21:53 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-3-22 21:46 |
|