Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

batch file - taskkill window spaces in its title name

I'm trying to run taskkill on a console window that has spaces in it's title. How can I pass this window title to taskkill. I have tried the following:

taskkill /fi "WINDOWTITLE eq Administrator: My Window Title"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've had similar problems, but found out a little bit more.

Problem

I have been trying to close a CMD window (run as administrator) which has set its own window title. So, run CMD as administrator and type:

title CMD with custom title

After much faffing, the following command showed me that setting the window title puts a leading space in front of the title! (No idea why.)

c:>tasklist /V /FI "WindowTitle eq Administrator*"

Image Name  PID Session Name Session# Mem Usage Status  User Name       CPU Time Window Title
========== ==== ============ ======== ========= ======= =============== ======== =====================================
cmd.exe    4304 Console             1   2,492 K Running MACHINEMy Name  0:00:00 Administrator:  CMD with custom title
                                                                                               ??
(scroll ??)                                                                               extra space

Solution

So to kill the process:

taskkill /F /FI "WindowTitle eq  Administrator:  CMD with custom title" /T
                                               ??
                                             bingo
  • /F - use the force.
  • /T - kill child-processes.

If you're banging your head in a similar fashion, I recommend using tasklist to poke around inside the process properties to make sure you're getting your filters right. This ballache brought to you by Windows 8.

Note: if the title contains quotation marks, just escape the nested ones:

title "CMD with custom title"

taskkill /F /FI "WindowTitle eq  Administrator:  "CMD with custom title"" /T

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...