Поделиться через


Keeping disks defragmented

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

  • Anonymous
    July 19, 2004
    This is by far one of the most useful blog posts I've ever read!

  • Anonymous
    July 19, 2004
    I would have to second that. Absolutely brilliant jay.

    How big are your hard drives?

    wink wink

  • Anonymous
    July 19, 2004
    The comment has been removed

  • Anonymous
    July 19, 2004
    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 :-)

  • Anonymous
    July 20, 2004
    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 :)

  • Anonymous
    July 20, 2004
    I'm surprised & happy to hear that this post has been so valuable to you. Cheers.

  • Anonymous
    July 20, 2004
    calling all CS people

  • Anonymous
    July 20, 2004
    http://martnet.com/~jfosler/

  • Anonymous
    July 21, 2004
    Excellent! I'm copying+pasting into notepad right now. More more!

  • Anonymous
    July 23, 2004
    defrag trukk

  • Anonymous
    July 23, 2004
    Automated Defrag for Windows XP

  • Anonymous
    July 28, 2004
    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...

  • Anonymous
    May 17, 2008
    PingBack from http://janiah.freeindiamusiconline.info/defragwhileworking.html

  • Anonymous
    June 15, 2009
    PingBack from http://mydebtconsolidator.info/story.php?id=6692