On a regular basis, top Microsoft executives answer readers' toughest technical questions about Windows-based systems. This installment of "Ask Microsoft" was answered by Mike Mercer, Microsoft Remote IT Services, GTS.
To submit a technical question for consideration, send an e-mail to editor@SearchWinComputing.com.
Question: Microsoft allows the option to run disk defrag ("defrag.exe") in command line. There are some switches, but none give you the power to schedule disk defragmentation. Is it possible to define a scheduled job with defrag.exe so that disk defragmentation won't require user intervention? More importantly, how could a user define a log for that job? Can you give an example?
Answer: With Windows 2003 and Windows XP the ability to run Disk Defragmenter from the command line (defrag.exe) was added. This functionality was missing from Windows 2000 and as a result you would have to resort to a scripted solution. In both Windows 2003 and Windows XP you can create a scheduled task using the 'Scheduled Tasks' wizard located in the Control Panel or from a command line using at.exe. In either case an example of the 'command' syntax is below:
To defrag E: drive;
Create a .bat file and paste the contents below inside:
defrag.exe -v e: >c:\temp\logfile.txt
The e: switch targets the e: drive for the defrag.
The -v switch enables verbose logging.
The >c:\temp\logfile.txt pipes the output to a file named logfile.txt.
Create the Schedule Task to launch the .bat file. The output will be located in c:\temp\logfile.txt once the job is completed.
Here is another option:
From the command line type the following to schedule a task:
at 12:00 c:\winnt\system32\cmd.exe /C "defrag.exe c: -v >c:\logfile.txt"
For more information, you can read How to Automate Disk Defragmenter Using Task Scheduler Tool in Windows XP at http://support.microsoft.com/default.aspx?scid=kb;en-us;555098.
-- Mike Mercer