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
I’m known for writing confusing batch files, so let’s go through it:
Turn off echo by default. If you want to see echo output, ‘set _echo=1’.
The ‘/d’ option means change the current drive, too. (Should be the default, but hard to change now.)
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.
Repeat for each parameter.
exit the batch script. (If you use ‘call :label’ then just exit this instance).
%~n0 means ‘just the base name of the current batch file’.
Next, add a scheduled task:
Now you’ll have a new task in the scheduled tasks list. Time to verify it.
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.