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.