Keeping disks defragmented

Published 19 July 04 02:25 PM

I want to keep my disks defragmented, with these constraints:

·         Don’t consume the disk IO resources while I’m working

·         Don’t make me remember to launch defrag before I go home

·         Defrag all my disks, not just one (Windows defragger only does one at a time)

·         Work correctly even if I run as normal user

 

 

To support this, I wrote a small script to run the defragger.  Here it is:

@if "%_Echo%" == "" echo off

 

if "%1" == "" goto Usage

 

     :next

 

cd /d %1\

 

echo defragmenting %1 > defrag.log

defrag -v -f %1 >> defrag.log 2>&1

 

shift

if NOT (%1) == () goto next

 

goto :EOF

 

 

     :Usage

 

echo pass volumes on the command line:

echo.

echo     %~n0 c: d: c:\mount_point

 

goto :EOF

 

 

I’m known for writing confusing batch files, so let’s go through it:

 

@if "%_Echo%" == "" echo off

 

Turn off echo by default.  If you want to see echo output, ‘set _echo=1’.

 

cd /d %1\

 

The ‘/d’ option means change the current drive, too.  (Should be the default, but hard to change now.)

 

defrag -v -f %1 >> defrag.log 2>&1

 

Run the defragger.  ‘-v’ means generate verbose output about the number of fragmented files before & after, etc.  Redirect it all to defrag.log, which will be (normally) be written to the root of the drive.

 

shift

if NOT (%1) == () goto next

 

Repeat for each parameter.

 

goto :EOF

 

exit the batch script.  (If you use ‘call :label’ then just exit this instance).

 

echo     %~n0 c: d: c:\mount_point

 

%~n0 means ‘just the base name of the current batch file’.

 

Next, add a scheduled task:

 

  1. Control Panel -> Scheduled Tasks
  2. File->New->Scheduled Task (Don’t use the Add Scheduled Task wizard)
  3. Give it a name (“defrag”).
  4. RClick, Properties:

 

  1. Set the schedule as it suits you.  I told it to abort my job after 1 hour, just in case something is really broken. 
  2. OK.  You’ll be prompted for local admin credentials

 

Now you’ll have a new task in the scheduled tasks list.  Time to verify it.

 

  1. RClick on the task, Run
  2. Watch its ‘Status’ change.  You want it to say ‘Running’ for at least a minute.  Anything else & something is broken.  Take a look at Advanced->ViewLog for clues.

 

The first time this ran, it was aborted after an hour. The next night it ran again & completed.  I think I’ll leave it like that.

 

 

Comments

# Sean Chase said on July 19, 2004 2:41 PM:
This is by far one of the most useful blog posts I've ever read!
# Sean Malloy said on July 19, 2004 5:30 PM:
I would have to second that. Absolutely brilliant jay.

How big are your hard drives?

*wink* *wink*
# Shawn said on July 19, 2004 6:25 PM:
Terrific blog! As the others have mentioned, this is probably one of the most useful blog posts I've ever read.

It's too bad that the Win2k defragger doesn't accept command line arguements. :(
# Jerryp said on July 19, 2004 11:08 PM:
You may want to add an additional defrag statement - just with the drive and -b

-b is "not documented" and will not show any output - even with -v. it moves those start up executeables to the beginning of the disk.

rumors say XP does it every other day itself - but just to be sure :-)
# Andrew Clarke said on July 20, 2004 8:23 AM:
Another option, which I've been using for a few years now, is diskeeper (http://www.execsoft.com) which meets all your criteria and more. My favourite feature is that it monitors your drives and will kick off a small defrag when it feels it needs to, at idle priority so it doesn't impact what you're doing.

It has other fun things like boot time defragging for those hard to reach files, but I use them rarely. Highly recommended.

(not a paid shill for execsoft, though if they want to throw me a free upgrade when the new version comes out, they can :)
# jaybaz [MS] said on July 20, 2004 9:56 AM:
I'm surprised & happy to hear that this post has been so valuable to you. Cheers.
# TrackBack said on July 20, 2004 10:58 AM:
calling all CS people
# TrackBack said on July 20, 2004 10:58 AM:
http://martnet.com/~jfosler/
# John Schroedl said on July 21, 2004 10:11 AM:
Excellent! I'm copying+pasting into notepad right now. More more!
# TrackBack said on July 23, 2004 10:13 AM:
defrag trukk
# TrackBack said on July 23, 2004 10:14 AM:
Automated Defrag for Windows XP
# Homer Simpson said on July 28, 2004 9:28 AM:
From the doh! department: be sure to not call your defrag command file c:\defrag.cmd.

It'll hide the defrag.exe and you'll end up in a recursive loop with a very large log file...
# defrag while working said on May 17, 2008 1:14 PM:

PingBack from http://janiah.freeindiamusiconline.info/defragwhileworking.html

# jaybaz MS WebLog Keeping disks defragmented | debt consolidator said on June 15, 2009 4:23 PM:

PingBack from http://mydebtconsolidator.info/story.php?id=6692

New Comments to this post are disabled
Page view tracker