Home Businesses, Business Home Users Downloads & Links Contact Us
CompuTelecom Phone Systems CompuTelecom Phone Systems Computers Defrag Scripts

Compu-Docs

Computer Repair & Services for NJ New Jersey Businesses & Home Users


Compu-Docs Windows 2000 & XP

Disk Defragmenter Scripts

 

If you would like to donate to our site click here:

Windows 2000 and XP have a built in defragmenter program, which is based on the popular Diskeeper software from Executive Software. The full version of Diskeeper will allow you to defrag remote disks, defrag your page file during bootup and it also has a built in scheduler feature. Those features are not available from the version that is included with Windows 2000. Microsoft even states in it's Knowledge Base article Q227463 that the Defragmenter program for Windows 2000 cannot be scripted to run scheduled defrags. 

That's true up until now.

A Google search turned up This Article at Windows & .Net Magazine. The code available for download there needs to be saved with a .vbs file extension and will only do 1 drive (your C: drive). I've added to the code to Defragment additional drives as follows. Windows XP is a little more user friendly in that it does have a command line feature (Defrag.exe) so, administrators can write scripts to run it at will or to schedule it to run during off hours. The vbs files below are for Windows 2000 and the bat (batch) files are for Windows XP. The files are in zip format for downloading.

The script between the horizontal lines below for Windows 2000 can be copied and pasted into Notepad and saved as Defrag.vbs or any name you wish. I've named my scripts Dfrg1prt.vbs through Dfrg4prt.vbs for Windows 2000 and Dfrg1prt.bat through Dfrg4prt.bat for Windows XP You then can use Scheduled Tasks in Control Panel to run for example Dfrg1prt.vbs (for 1 W2K partition) or Dfrg1prt.bat (for 1 XP partition) at a convenient time. You can also just create a desktop shortcut to the file to start Disk Defragmenter at any time you wish. The vbs files have been tested on the final English versions of Windows 2000 Pro and Server only. The bat files have been tested on the final English versions of Windows XP Home & Pro

The version shown between the horizontal lines below for Windows 2000 only, will defrag 4 partitions. If you just copy the black text including the last 2 End commands, it will defrag just the C: partition. If you copy the black & blue text, it will defrag the C: & D: partitions. If you copy the black, blue & red text, it will defrag the C:, D: & E: partitions. If you copy the black, blue, red & green text (the whole script), it will defrag the C:, D:, E: & F: partitions. Save your text file with whatever name you wish and then rename it so it has a .vbs file extension. By using this code or the files available for downloading you agree to the Warranty Disclaimer

WARRANTY DISCLAIMER
THE SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, COMPU-DOCS FURTHER DISCLAIMS ALL WARRANTIES, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE PRODUCT AND DOCUMENTATION REMAINS WITH RECIPIENT. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL COMPU-DOCS BE LIABLE FOR ANY CONSEQUENTIAL, INCIDENTAL, DIRECT, INDIRECT, SPECIAL, PUNITIVE, OR OTHER DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OUT OF THIS AGREEMENT OR THE USE OF OR INABILITY TO USE THE PRODUCT, EVEN IF COMPU-DOCS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

DISCLAIMER OF WARRANTY
THIS SOFTWARE AND THE ACCOMPANYING FILES ARE PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND WHETHER EXPRESSED OR IMPLIED.

IN NO EVENT SHALL THE AUTHOR BE HELD LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS OR PERSONAL INFORMATION, OR ANY OTHER LOSS ARISING FROM THE USE OR INABILITY TO USE THE SOFTWARE.

Or you can also download one of the following completed scripts if you agree to the Warranty Disclaimer.

WINDOWS 2000 SCRIPTS

Note: Unzipping the following files will create a Defrag folder. Inside the folder is the script and a shortcut (you can copy the shortcut to your Desktop if you so desire). Place the Defrag folder at the root of your C: drive for the shortcut to work properly.

For one Windows 2000 partition (C drive only) Defrag2K1Part.zip 1KB  I Agree 

For two Windows 2000 partitions (C & D drives only) Defrag2K2Part.zip 1KB I Agree

For three Windows 2000 partitions (C, D & E drives only) Defrag2K3Part.zip 1KB I Agree

For four Windows 2000 partitions (C, D, E & F  drives only) Defrag2K4Part.zip 1KB I Agree

You can also use the NEW! AUTODEFRAG.EXE executable file found below.

WINDOWS XP SCRIPTS

Note: Unzipping the following files will create a Defrag folder. Inside the folder is the script and a shortcut (you can copy the shortcut to your Desktop if you so desire). Place the Defrag folder at the root of your C: drive for the shortcut to work properly.

For one Windows XP partition (C drive only) DefragXP1Part.zip 1KB  I Agree 

For two Windows XP partitions (C & D drives only) DefragXP2Part.zip 1KB I Agree

For three Windows XP partitions (C, D & E drives only) DefragXP3Part.zip 1KB I Agree

For four Windows XP partitions (C, D, E & F  drives only) DefragXP4Part.zip 1KB I Agree

You can also use the NEW! AUTODEFRAG.EXE executable file found below.

NEW! AUTODEFRAG.EXE PROGRAM: Thanks goes to Bob Askey for taking the code from this page and creating an executable file which automatically figures out how many partitions you have. It works on both Windows 2000 Pro & Server and Windows XP Home & Pro. A plus for XP users is that it runs the GUI version of Disk Defragmenter rather than the command line version.

There are only 2 bugs found in the Autodefrag.exe program. One is if the computer has a hidden partition (Dell uses a hidden partition), the last partition will be defragmented twice, which is no big deal. The other is if you have memory card readers, thumb drives or memory sticks that show up as drives in My Computer or Windows Explorer, then the last partition will be repeatedly defragmented by the number of extra drive letters. In any case it will finally get to defragmenting the C: drive.

Note: Unzipping the following files will create a Defrag folder. Inside the folder is the executable file and a shortcut (you can copy the shortcut to your Desktop if you so desire). Place the Defrag folder at the root of your C: drive for the shortcut to work properly.

For Windows 2000 and XP, Download AutoDefrag.zip 9KB I Agree

NOTE FOR ALL USERS: Use either Scheduled Tasks in Control Panel and browse to the file to schedule the program to run or create a desktop shortcut to the file to run it any time you wish. You can also just put the script itself on your desktop and run it from there.

You should not run these scripts with any other application running in the foreground at the same time. For example, if you double click the file in Windows Explorer, then Windows Explorer may pick up some of the keystroke commands and cause the script not to run properly. So make sure nothing else is running (viewable) on your desktop when you run the script. The best way to do this is to create a shortcut to the file or use Scheduled Tasks. Also please note that Disk Defragmenter requires a person with administrator rights to run it.

NOTE FOR ALL WINDOWS 2000 USERS: The file ShellIconCache frequently gets fragmented and is not defragged by Disk Defragmenter. You can defrag it beforehand by running the batch file Shell.bat, which I have zipped into Shell.zip.

For Windows 2000 Only, Download Shell.zip 1KB  I Agree

NOTE FOR WINDOWS 2000 ADMINISTRATORS: Do you want a regular user to be able to run the scripts? Click Here.

NOTE FOR ANTI-VIRUS USERS: When users running Norton Anti-Virus run any kind of vbs (script) file, an Alert warning box may  pop up. For the "Action:" you should choose "Authorize this script" from the drop down menu. In the future, when you run the script, you will not receive the Alert warning box again. Other Anti-Virus programs may also prompt you to allow the scripts to run.

If you would like to donate to our site click here:

WINDOWS 2000 SCRIPT FOR 4 PARTITIONS IS SHOWN BELOW:


'This script launches defrag and sends keys to the UI in order to automate the defrag
'process for 4 partitions, Edited by Compu-Docs to defrag additional drives.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for 2 seconds
WshShell.Run "dfrg.msc"
WScript.Sleep 2000

'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

'1 Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 500

'1 Send keys to move to top of drive/partition table.
WshShell.SendKeys "{TAB}{HOME}"
WScript.Sleep 501

'1 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 502

'1 Send a D to start the defrag
WshShell.SendKeys "D"

'1 Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5001
Wend

'1 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 503

'1 Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 504

'1 Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 505

'2 Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 506

'2 Send key to move to next drive/partition.
WshShell.SendKeys "{DOWN}"
WScript.Sleep 507

'2 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 508

'2 Send a D to start the defrag
WshShell.SendKeys "D"

'2 Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5002
Wend

'2 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 509

'2 Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 510

'2 Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 511


'3 Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 512

'3 Send key to move to next drive/partition.
WshShell.SendKeys "{DOWN}"
WScript.Sleep 513

'3 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 514

'3 Send a D to start the defrag
WshShell.SendKeys "D"

'3 Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5003
Wend

'3 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 515

'3 Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"

Wscript.Sleep 516

'3 Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 517


'4 Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 518

'4 Send key to move to next drive/partition.
WshShell.SendKeys "{DOWN}"
WScript.Sleep 519

'4 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 520

'4 Send a D to start the defrag
WshShell.SendKeys "D"

'4 Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5004
Wend

'4 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 521

'4 Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 522

'4 Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 523

'End - Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 700

'End - Send an ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"


 

You Are visitor number Hit Counter to this Web page.

Home Businesses Home Users Downloads & Links Contact Us
CompuTelecom Phone Systems CompuTelecom Phone Systems Computers Defrag Scripts

All trademarks used are properties of their respective owners.
All rights reserved. Copyright © 2003 - 2005 by CompuDocs NJ, Inc.

Last modified: June 19, 2014