Hands-Free Disk Defragmenting
Pages: 1, 2
Click Next and select the check box and click Finish to open the advanced properties for the task. Select the Task tab on this properties sheet:
Figure 4: Configuring advanced task properties.
Now you need to edit the command string in the Run box to indicate what drive you want to defrag. For example, to defrag drive C: on your machine, change the command string to look like this:
You can also force defragmentation to occur even if there is insufficient disk space (Disk Defragmenter normally won't run unless there is at least 15 percent free space on the drive being defragmented). To do this, add the
/f (for force) switch to your command string as follows:
C:\WINDOWS\system32\defrag.exe c: /f
You may also want to create a log of your defragmentation task. You can do this by redirecting the output of the command to a text file:
C:\WINDOWS\system32\defrag.exe c: > c:\mylogs\defrag.log
If you want detailed logging you can use verbose mode:
C:\WINDOWS\system32\defrag.exe c: /v > c:\mylogs\defrag.log
Finally, you can select the other tabs on the properties sheet to configure additional advanced settings if they are needed. For example, you could configure the task so it will run only if the computer has been idle for at least 30 minutes, or wake the computer if it is asleep to run the task.
Defragging Multiple Volumes
What if you have several disk volumes you want to defragment? One way of doing this would be to create a separate scheduled task for each volume and stagger these tasks in time so they don't all happen simultaneously. For example, you could defragment your C: drive on Monday, your D: drive on Tuesday, and so on.
Another approach is to create a batch file that contains a series of defrag commands like this:
C:\WINDOWS\system32\defrag.exe c: C:\WINDOWS\system32\defrag.exe d: C:\WINDOWS\system32\defrag.exe e:
Then schedule the batch file to run using the Scheduled Tasks wizard and each task will occur in sequence.
You can also schedule tasks using the
schtasks.exe command as follows:
schtasks /create /tn "Defrag Drive C" /tr "C:\WINDOWS\system32\defrag.exe c: /v > c:\mylogs\defrag.log" /sc monthly /mo FIRST /d MON /st 02:00:00 /u MYBOX\Administrator /p Pa$$w0rd /ru SYSTEM
This command will create a task named "Defrag Drive C" which runs defrag in verbose mode, logging output to a text file. The task will run on the first Monday of each month at 2 a.m. and will run
defrag.exe using local admin credentials for the computer. The task itself will run under the privileges of the System account.
Before You Defrag
Finally, before you defrag your drive, it's a good idea to make sure you've cleaned it by deleting any unneeded files such as Internet Explorer's temporary files and files moved to your Recycle Bin. You can do this manually using the Disk Cleanup utility, but you can also automate this process so it takes place just before you defragment your drives. To find out how to automate disk cleaning, see Disk Cleanup Hacks.
Return to the Windows DevCenter.