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-07-15 00:25
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » --- -- End all processes except the main ones ? View 2,821 Replies 15
Original Poster Posted 2007-04-19 04:38 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
--- -- I want to end all processes except the main ones, like below, but there's a problem. Please give me some guidance, experts!

for %%i in ('wmic process get name') do
( for /f %%b in (smss.exe,csrss.exe,winlogon.exe,services.exe,lsass.exe,svchost.exe,Explorer.exe) do if /i not "%%i"=="%%b" taskkill /f /im %%i)
pause
Floor 2 Posted 2007-04-19 05:04 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
欢迎你到批处理爱好者联盟QQ群:18023953
Floor 3 Posted 2007-04-19 05:29 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
Attachments
未命名.bmp
Floor 4 Posted 2007-04-19 05:36 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
for /f "skip=1 delims=" %%i in ('wmic process get name') do (
for %%b in (smss.exe,csrss.exe,winlogon.exe,services.exe,lsass.exe, svchost.exe,Explorer.exe) do if /i not %%i== %%b taskkill /im %%i /T
)
pause


--- -- Is there something wrong with this structure itself?

-- - eg: the problem of not services.exe== smss.exe !! Then taskkill /im services.exe /t (actually, I want to use pskill to end processes),
Floor 5 Posted 2007-04-21 06:40 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
--- -- Brothers, still no solution?
Floor 6 Posted 2007-04-21 06:45 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Check your code carefully
Floor 7 Posted 2007-04-21 08:53 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
for /f "skip=3 delims=" %%i in ('wmic process get name') do (
for %%b in (smss.exe,csrss.exe,winlogon.exe,services.exe,lsass.exe,

svchost.exe,Explorer.exe) do if /i not %%i==%%b taskkill /im %%i /t

)
pause


for /f "skip=3 delims=" %%i in ('wmic process get

name') do ( if /i "%%i"==smss.exe if /i "%%i"==csrss.exe if /i "%%

i"==winlogon.exe if /i "%%i"==services.exe if /i "%%i"==lsass.exe

if /i "%%i"==svchost.exe if /i "%%i"==Explorer.exe taskkill /f /im "%%i" )

pause

This one also errors out!!
Please advise!
Floor 8 Posted 2007-04-21 09:12 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
Wrong, change all the if /i "%%i"== to if /i not "%%i"==
Floor 9 Posted 2007-04-21 10:01 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Remove that delims= part, it's capturing tabs
Floor 10 Posted 2007-04-22 07:44 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
--- --

--- -- (1)

for /f "skip=3 " %%i in ('wmic process get name') do (
for %%b in (smss.exe,csrss.exe,winlogon.exe,services.exe,lsass.exe,

svchost.exe,Explorer.exe) do if /i not %%i==%%b taskkill /im %%i /t

)
pause

Where the error is: for example, if the process name obtained by 'wmic process get name' is services.exe, when processing the if statement, it becomes if /i not services.exe ==smss.exe taskkill /im services.exe /t, then in the next loop if /i not services.exe ==csrss.exe taskkill /im services.exe /t ……

As a result, it can't preserve smss.exe,csrss.exe,winlogon.exe,services.exe,lsass.exe,

svchost.exe,Explorer.exe (on the condition that I'm using pskill)



--- -- (2)

for /f "skip=3" %%i in ('wmic process get name') do ( if /i not "%%

i"==smss.exe if /i not "%%i"==csrss.exe if /i not "%%i"==winlogon.exe

if /i not "%%i"==services.exe if /i not "%%i"==lsass.exe if /i not "%%

i"==svchost.exe if /i not "%%i"==Explorer.exe taskkill /f /im "%%i")

pause

Where the error is:

Again taking services.exe as an example, when processing the if statement, I think it should be handled like this:

if /i not services.exe ==smss.exe if /i not services.exe ==csrss.exe if /i not services.exe==winlogon.exe …… if /i not services.exe==Explorer.exe taskkill /f /im services.exe


It should pass through the filtering of the if statements one by one, but in reality it only processes the first if statement:
if /i not services.exe==smss.exe then it executes taskkill /f /im services.exe right away!
I don't understand!

--- -- (3)

Another idea is to use 'wmic process get name' to list the processes, then find
smss.exe,csrss.exe,winlogon.exe,services.exe,lsass.exe,svchost.exe,Explorer.exe and remove them, leaving the processes that should be ended. In one sentence, filtering. Not sure whether that's feasible?

(I don't know how to filter)

Please advise, experts, or if there is any other method that's fine too, many thanks!!
Floor 11 Posted 2007-04-22 07:49 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
According to your requirement


for /f "skip=3" %i in ('wmic process get name^|findstr /v /i "smss.exe csrss.exe winlogon.exe services.exe lsass.exe svchost.exe explorer.exe"') do echo %i
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
htysm +8 2007-04-22 08:19
Floor 12 Posted 2007-04-22 08:19 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
The poster above, brilliant.
欢迎你到批处理爱好者联盟QQ群:18023953
Floor 13 Posted 2007-04-22 08:23 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 313
Posts 162
Joined 2007-04-02 06:45
19-year member
UID 83693
Gender Male
Status Offline
--- -- for /f "skip=3" %%i in ('wmic process get name^|findstr /v /i "smss.exe csrss.exe

winlogon.exe services.exe lsass.exe svchost.exe explorer.exe"') do echo %i >>af.txt
pause

It works, thanks!

I also want to ask one more question. If I make a batch file using

for /f "skip=3" %%i in ('wmic process get name^|findstr /v /i "smss.exe csrss.exe

winlogon.exe services.exe lsass.exe svchost.exe explorer.exe"') do taskkill /f /imi %i

to end processes, even cmd.exe gets ended, and there is no interpreter anymore. Will the commands after that still run?

attrib -s -h d:\* /s
pause


Also, will taskkill.exe itself get ended?
Is the process list obtained by 'wmic process get name' a list at one particular moment, i.e. a static list, or will it change as processes are added and removed, assuming during the loop above (before it finishes), for example if another program is opened, will that program be ended too?
Floor 14 Posted 2007-04-22 08:40 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
You can observe these questions carefully yourself. I haven't actually tried it, but it must be a static list
Floor 15 Posted 2007-04-22 08:59 ·  中国 广东 广州 电信
中级用户
★★
Credits 259
Posts 112
Joined 2006-09-18 04:55
19-year member
UID 62928
Gender Male
Status Offline
Hehe.. pretty interesting!
Forum Jump: