China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 14:20
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to detect a specific process View 2,023 Replies 12
Original Poster Posted 2007-02-17 03:18 ·  中国 山东 青岛 联通
初级用户
Credits 20
Posts 7
Joined 2007-02-17 02:38
19-year member
UID 79666
Gender Male
Status Offline
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 ]
Floor 2 Posted 2007-02-17 03:28 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
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
Floor 3 Posted 2007-02-17 03:31 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@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
Floor 4 Posted 2007-02-17 03:42 ·  中国 山东 青岛 联通
初级用户
Credits 20
Posts 7
Joined 2007-02-17 02:38
19-year member
UID 79666
Gender Male
Status Offline
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.
Floor 5 Posted 2007-02-17 03:46 ·  中国 山东 青岛 联通
初级用户
Credits 20
Posts 7
Joined 2007-02-17 02:38
19-year member
UID 79666
Gender Male
Status Offline
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!
Floor 6 Posted 2007-02-17 05:14 ·  中国 广东 湛江 电信
高级用户
★★★
Credits 959
Posts 311
Joined 2006-04-11 14:08
20-year member
UID 53665
Gender Male
From 广东-LianJiang
Status Offline
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
κχυμγνξοθπρωψιαδλεηφβτζσ┬╀┾┳┞┯┰┱┣┲┳╂╁│├┟┭┠这是什么??这就是我的人生
Floor 7 Posted 2010-03-22 21:15 ·  中国 浙江 杭州 电信VDSL
初级用户
Credits 21
Posts 10
Joined 2007-11-20 22:37
18-year member
UID 103273
Gender Male
Status Offline
Can the code on floor 3 find the specified process? You must have made a mistake.
Floor 8 Posted 2010-03-22 21:27 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Floor 9 Posted 2010-03-22 21:28 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Floor 10 Posted 2010-03-22 21:35 ·  中国 浙江 杭州 电信VDSL
初级用户
Credits 21
Posts 10
Joined 2007-11-20 22:37
18-year member
UID 103273
Gender Male
Status Offline
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!
Floor 11 Posted 2010-03-22 21:40 ·  中国 浙江 杭州 电信VDSL
初级用户
Credits 21
Posts 10
Joined 2007-11-20 22:37
18-year member
UID 103273
Gender Male
Status Offline
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?
Floor 12 Posted 2010-03-22 21:43 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Floor 13 Posted 2010-03-22 21:46 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Forum Jump: