Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


May 2007

Automate the Windows 2003 Defragmenter Without Paying Extra


RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Defragmentation is a great way to keep workstations and servers running at their best performance. Windows Server 2003 comes with a defragmenter: dfrgntfs.exe. However, you can't automate this defragmenter unless you purchase a program such as Diskeeper. I didn't have money for such a program in my budget, so I created and scheduled a batch file named Defrag.bat.

As Listing 1 shows, Defrag.bat is a simple program. It uses the defragmenter's command-line interface (defrag.exe) to run the defragmenter against each drive. However, I wasn't happy with Defrag.bat for two reasons. First, I had to let the batch file run while being left logged on with my administrator ID. Although I locked the screen for security reasons, I didn't want to leave myself logged on all the time. Second, I wanted more automation.

So, I did some digging around and found a means to automate the procedure. I found that I could use the Windows scheduler but in a different way that I didn't know was possible: I could use the AT command with a batch file.

I created a new batch file, Defrg. bat, which Listing 2 shows. Like Defrag.bat, Defrg.bat runs defrag.exe. However, Defrg.bat has a few more features than Defrag.bat. I included code that documents when the defragmenter starts and ends in a log file. I also added code that ports the defragmenter's screen output to the same log file (with some titles in between) to record which drives are being defragmented. That way, I can easily check to see whether the defragmentation operation ran and whether any errors occurred.

To use the new script, I log on to the server with my administrator ID, open a command-shell window, and run command

at 08:00pm /every:M,T,W,Th,F
  f:\Defrg.bat 

(Although this command appears on several lines here, you would enter it on one line in the command-shell window.) This command creates a new scheduled item in Scheduled Tasks that runs Defrg.bat every weeknight at 8 p.m. (which is before our backup runs).

With this new batch file, I don't need to be logged on for it to run. Because Defrg.bat is running as a system process, the defragmentation operation is performed in the background (i.e., no window comes up), but Task Manager will show that defrag.exe and dfrgntfs.exe are running. After Defrg.bat finishes, the scheduler will show 0x0 for a successful execution. However, I always check the log file to make sure no problems were encountered.

To use Defrg.bat, you simply need to replace f:\defrag.log in the code at callout A in Listing 2 with the pathname to your log file. The AT command and batch file work on Windows Server 2003, Windows XP Professional, and Windows XP Home Edition. I recommend that you make the batch file a read-only, hidden file. That way, no one can edit it so that it damages your computers when the scheduled batch runs.
—Daniel L. Gillard

See Associated Figure

End of Article



Reader Comments
My article "Extending Windows XP's Disk Defragmenter" (November 2004, InstantDoc #43924) provides a VBScript script called XDefrag.vbs that works on Windows XP and later. XDefrag.vbs provides defrag timing and event logging. Also, unlike this shell script, it can detect and defragment all local hard drives, so you don't have to edit it if your local drive letters are different.

HTH,

Bill

AbqBill April 30, 2007 (Article Rating: )


Well Bill, while your script may sound good it is never the less not accessible unless you have a subscription to yet another one of Penton's publications.

In their zeal to make money all they have done in the past few years is encourage more subscribers to cancel. Lets see the business plan now is to have smaller publications, put the good content on the web (thus defeating the purpose of a magazine) and having one subscription for every conceivable subject. Oh ya I forgot, write articles that only pertain to IT shops with 10,000 computers and multi-million dollar budgets.

In this case the web is going to kill traditional publications because they forgot who their audience is and what they want.

wtb5 May 28, 2007 (Article Rating: )


I find the article well explain and very usefull.

carlosgarcia July 19, 2007 (Article Rating: )


While I tend to agree with wtb5's comments, I found the article most useful and a great tip for those who DON'T have huge budgets for Server defraggers. More tips like this will be most welcome, thanks.

scoop1970 July 19, 2007 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

More fun TechEd 2005 Resources

Kevin points out some more TechEd resources ...

Outlook Tips and Techniques

Read about hiding items, merging appointments, multiple windows, creating views, permissions, sending Outlook items to outside recipients, Send As permission, Inbox Assistant, tricks for rules, and tips for obtaining Microsoft Knowledge Base articles. ...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

Related Events Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Microsoft Exchange & Windows Connections event returns to Las Vegas Nov 10 - 13
Connections returns to Las Vegas for this exciting event where each attendee will receive SQL Server 2008 standard with 1 CAL. Co-located with Microsoft ASP.NET, SQL Server, and SharePoint Connections with over 250 in-depth sessions.

Free Online Event! Virtualization:Get the Facts!
Register now and attend this free, live in-depth online conference on November 13 and 20, 2008, produced by Windows IT Pro. All registrants are eligible to receive a complimentary one-year digital subscription to Windows IT Pro (a $49.95 value)!

Check Out Hyper-V Video on ITTV
Watch Karen Forster's interview on Hyper-V's performance on ITTV.net.

Ease Your Scripting Pains with the Flexibility of PowerShell!
Join MVP Paul Robichaux on December 11, 2008 at 11:00 AM EDT as he equips you with PowerShell basics in 3 introductory lessons, each followed by a live Q&A session—all on your own computer!

PASS Community Summit 2008 in Seattle on Nov 18-21
The don’t-miss event for Microsoft SQL Server Professionals. Register now and you’ll enjoy top-notch Microsoft and Community speakers and more.



Solving PST Management Problems
In this white paper, read about the top PST issues and how to administer local/network PST Files.

Get Protected -- Data Protection Manager 2007
Protect your virtualized environment with Data Protection Manager

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Maximize Your SharePoint Investment: Get Your Data Moving
Watch this web seminar now to learn how to maximize your SharePoint investment! Join us as we take a look at the complex business of securing, accessing and managing vast amounts of information in a global network and various ways to get your data moving.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing