中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 16:02
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » How to detect a specific process
Printable Version  2,025 / 12
Floor1 gromancer Posted 2007-02-17 03:18
初级用户 Posts 7 Credits 20
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 ]
Floor2 everest79 Posted 2007-02-17 03:28
金牌会员 Posts 1,127 Credits 2,564
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
Floor3 slore Posted 2007-02-17 03:31
铂金会员 Posts 2,478 Credits 5,212
@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
Floor4 gromancer Posted 2007-02-17 03:42
初级用户 Posts 7 Credits 20
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.
Floor5 gromancer Posted 2007-02-17 03:46
初级用户 Posts 7 Credits 20
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!
Floor6 qasa Posted 2007-02-17 05:14
高级用户 Posts 311 Credits 959 From 广东-LianJiang
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
Floor7 longmarchw Posted 2010-03-22 21:15
初级用户 Posts 10 Credits 21
Can the code on floor 3 find the specified process? You must have made a mistake.
Floor8 Hanyeguxing Posted 2010-03-22 21:27
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
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.


[ Last edited by Hanyeguxing on 2010-3-22 at 21:37 ]
Floor9 Hanyeguxing Posted 2010-03-22 21:28
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
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.


[ Last edited by Hanyeguxing on 2010-3-22 at 21:36 ]
Floor10 longmarchw Posted 2010-03-22 21:35
初级用户 Posts 10 Credits 21
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!
Floor11 longmarchw Posted 2010-03-22 21:40
初级用户 Posts 10 Credits 21
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?
Floor12 Hanyeguxing Posted 2010-03-22 21:43
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
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 ]
Floor13 Hanyeguxing Posted 2010-03-22 21:46
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
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 ]
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023