Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon




Login
Register

© winscripter.com
1998-2004







Script: Defrag your Hard Drive

Posted by on Friday, December 24, 2004 (PST)

Script: Dan Casier sent me this script to automate defragging your hard drive. Good example of using WSH to control another application through AppActivate and SendKeys.

 

/**  Script:  DeFrag.js
*
*    Purpose: Calls Defrag and start defragmentation. It stops
*             after one hour.
*
*    Author:  Dan Casier
*    Date:    30 oct 2004
*    Web:     none
*    Note:    run on Windows 2000 Pro, server and XP. Windows 2003
*             not tested. Useful to defrag disks on server by night
*             used in sheduled tasks
*    Copyright 2004 Dan Casier
*
**/
// Title of Disk Defragmenter MMC
var appTitle = "Schijfdefragmentatie";  // Dutch
var appTitle = "Disk Defragmenter";  // English
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Run("%SystemRoot%\\System32\\dfrg.msc");
WScript.Sleep(100);
WshShell.AppActivate( appTitle );
WScript.Sleep(100);
//***to select next harddisk include next lines
//WScript.Sleep(500);
//WshShell.SendKeys("{TAB}");
//WScript.Sleep(500);
//WshShell.SendKeys("{DOWN}");

//start defragmentation
WshShell.SendKeys("%a");
WScript.Sleep(500);
WshShell.SendKeys("d");

//wait one hour
WScript.Sleep(1000*60*60);   //value is in milliseconds
//stops the program
WshShell.AppActivate( appTitle );
WshShell.SendKeys("{RIGHT}");
WScript.Sleep(500);
WshShell.SendKeys("{ENTER}");
WScript.Sleep(500);
WshShell.SendKeys("%{F4}");
 

Comments:

Windows 2003 server test
By DanC on Thursday, December 30, 2004 (PST)

The script is tested on a Windows 2003 server and works well.

 

 



WSH and ADSI Administrative Scripting

New Articles
  • List installed COM objects and associated ProgIDs
    Script: Lists all COM Objects and their associated ProgIDs (If available). Win32_ClassicCOMClassSetting

  • Script: File Rotator
    Script: Rotate files where the most current file has the lowest number in the archive. When files exceed the retention period, they are deleted. Typically used for log files, backups, etc..

  • Script: Create IIS Website and DNS record
    Script: Dan Casier sent me this script that will create a website and appropriate DNS record. The script is intended for Windows 2000 Server with local DNS and necessary DNS mof installed.


  • Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon