Page 1 of 4

Is there a command to close a running program

Posted: 21 May 2011 07:28
by Huidong
I don't see such a command myself, so I wonder if it's possible to terminate a process on demand by scripting? Thanks!

Re: Is there a command to close a running program

Posted: 21 May 2011 07:46
by serendipity
Huidong wrote:I don't see such a command myself, so I wonder if it's possible to terminate a process on demand by scripting? Thanks!
Yes, via run or cmd.
Example:
Open notepad and run this:
::run "cmd /c TSKILL notepad";
notepad should close.
This is for XP though, not sure if this works on win7 or not.

Re: Is there a command to close a running program

Posted: 21 May 2011 07:55
by nas8e9
serendipity wrote:
Huidong wrote:I don't see such a command myself, so I wonder if it's possible to terminate a process on demand by scripting? Thanks!
Yes, via run or cmd.
Example:
Open notepad and run this:
::run "cmd /c TSKILL notepad";
notepad should close.
This is for XP though, not sure if this works on win7 or not.
TSKILL is part of Windows 7. Alternatively, psexec as part of the Sysinternals Suite can be used.

Re: Is there a command to close a running program

Posted: 21 May 2011 07:58
by Huidong
It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.

Re: Is there a command to close a running program

Posted: 21 May 2011 08:10
by serendipity
Huidong wrote:It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.
You can omit .exe and use defraggler. You get the process names from task manager's process.

::run "cmd /c TSKILL defraggler";

Re: Is there a command to close a running program

Posted: 21 May 2011 08:12
by Huidong
Ho that's so cool! Thank you!

Re: Is there a command to close a running program

Posted: 21 May 2011 08:18
by Huidong
Oh, one more question, how to use if...else... to close the program only if it's running? i.e. how to check if a program is running? Reason: I noticed that the command-line exe of defraggler (df.exe) does not work if the GUI exe is running, so after I wrote a command to defrag selected items, I'd like to add that check beforehand so that the GUI gets killed before attempting to run df.exe. Could you help?

Re: Is there a command to close a running program

Posted: 21 May 2011 08:29
by nas8e9
Huidong wrote:Oh, one more question, how to use if...else... to close the program only if it's running? i.e. how to check if a program is running? Reason: I noticed that the command-line exe of defraggler (df.exe) does not work if the GUI exe is running, so after I wrote a command to defrag selected items, I'd like to add that check beforehand so that the GUI gets killed before attempting to run df.exe. Could you help?
Sysinternals PSList can be used for this. It will return a errorlevel of 0 if the process is running and of 1 if it's not.

Code: Select all

pslist defraggler.exe
   if errorlevel 0 (                  //Running.
      tskill defraggler.exe
   ) else (                           //Implies errorlevel 1 i.e. not running.
      //Alternative operation.
   ) 
Based on your description I wonder if it's necessary to actually test for a process running first before trying to terminate it; the TSKILL command will simply return an error and allow the script/batch file to continue?

Re: Is there a command to close a running program

Posted: 21 May 2011 08:45
by Huidong
serendipity wrote:
Huidong wrote:It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.
You can omit .exe and use defraggler. You get the process names from task manager's process.

::run "cmd /c TSKILL defraggler";
I tried the command above but defraggler is not killed! Why?

Re: Is there a command to close a running program

Posted: 21 May 2011 08:52
by Huidong
OMG, even run "cmd /c TSKILL notepad"; stops working!! Is my computer wrong or sth? I believe that I just tried that script and it worked once!!! Ghost!

Re: Is there a command to close a running program

Posted: 21 May 2011 08:54
by serendipity
Huidong wrote:
serendipity wrote:
Huidong wrote:It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.
You can omit .exe and use defraggler. You get the process names from task manager's process.

::run "cmd /c TSKILL defraggler";
I tried the command above but defraggler is not killed! Why?
I thought "Ho that's so cool!" killed defraggler before.
Can you open task manager and see what the image name is underprocess? I just renamed defraggler.exe to killmeplz.exe and killed it using this:

::run "cmd /c TSKILL killmeplz";

Re: Is there a command to close a running program

Posted: 21 May 2011 09:00
by Huidong
Now I can't even kill notepad! It feels like I'm in a horror move! Any idea what happened to my computer? I just killed notepad once, I think.

Re: Is there a command to close a running program

Posted: 21 May 2011 09:01
by Huidong
What's "image name"? The process name is just Defraggler.exe, is that what you're asking?

Re: Is there a command to close a running program

Posted: 21 May 2011 09:13
by serendipity
Huidong wrote:What's "image name"? The process name is just Defraggler.exe, is that what you're asking?
then ::run "cmd /c TSKILL defraggler"; should work.

You can always open command prompt and test it from there first.

First type "tasklist" to see if you see the process "defraggler.exe" there.

if so use:
TSKILL defraggler

does it work?

Re: Is there a command to close a running program

Posted: 21 May 2011 09:17
by Huidong
TSKILL defraggler in cmd worked!
But again,
run "cmd /c TSKILL defraggler"
does not work!