jamesoff.net




23
Oct
2007
0

Checking for another process before starting JKDefrag

A user on the JKDefrag forum was asking about not running JKDefrag if another process is running (as they don’t play nice together), so I whipped this up.

You’ll need PsList from SysInternals in your path somewhere.

Change the three SET lines to point to jkdefrag, give the parameters you want to use, and the name of the program to avoid. (Get the name of the program from PsList.)

The “>2 NUL” sends stderr to NUL to avoid printing the PsList banner. It works on Vista, not sure about XP/2003. If it doesn’t just remove it and live with the PsList banner showing up.

Making this check for more than one process is an exercise for the reader. Also, you could use this with PsExec to check for the process on a remote host and then launch JKDefrag over there.

@echo off

REM Only run jkdefrag if another process is not running

REM Path to jkdefrag
SET JKDEFRAG=c:\jkdefrag\jkdefrag.exe

REM Options for jkdefrag
SET JKDEFRAGOPTS=-a 3

REM Process we don't want running
SET AVOID=explorer

 
REM Here we go!
 
pslist %AVOID% > NUL 2> NUL
if %ERRORLEVEL% == 0 goto skip

REM If we made it here, pslist didn't find the process
echo Couldn't find %AVOID% running, starting jkdefrag...
%JKDEFRAG% %JKDEFRAGOPTS%
goto end
 
:skip
echo %AVOID% is running, not running jkdefrag
 
:end

Add a comment