TASKKILL: how to skip unwanted task for better performance?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

TASKKILL: how to skip unwanted task for better performance?

#1 Post by tinfanide » 29 Jan 2012 11:46

The workflow:

Code: Select all

@ECHO OFF

:: In M drive, open the files "1.txt, 2.doc, txt\3.txt"
:: There are other files not opened e.g. "4.txt, 5.doc" many files...
:: Kill those tasks opened

:: Loop thru all the files in M drive
FOR /R "M:\" %%B IN (*.*) DO (
:: Find the 2nd column "PID" in TASKLIST where WINDOWTITLE == those (all) files in M Drive
   FOR /F "tokens=2" %%C IN ('TASKLIST /NH /FI "WINDOWTITLE eq %%~nxB*"') DO (

:: If found, they will be killed according to their PIDs
      IF NOT %%C == INFO: TASKKILL /PID %%C
:: but it also returns those unopened tasks and returns ERROR messages
   )
)

PAUSE


This code is to loop thru a portable device for any tasks located in the drive running because I often come across a situation where the computer says "Unable to unplug the drive" because there are tasks running.

Anyway, my question is:
I want to speed up the process by skipping those unfound tasks that returns an error message:
"ERROR: ..."
It takes long time to loop through those run tasks in the drive before the computer finds the tasks opened.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: TASKKILL: how to skip unwanted task for better performan

#2 Post by Squashman » 29 Jan 2012 13:06

You may want to refrain from using double colons for comments inside an expression surrounded by parenthesis.

Post Reply