Microsoft KB Archive/294743

= How to provide event logging for the Disk Defragmenter utility with Windows Script Host =

Article ID: 294743

Article Last Modified on 1/15/2006

-

APPLIES TO


 * Microsoft Windows XP Home Edition
 * Microsoft Windows XP Professional

-



This article was previously published under Q294743





SUMMARY
This article discusses how to provide event logging for the Disk Defragmenter (Defrag.exe) utility with Windows Script Host. This command-line Disk Defragmenter utility that is included with Microsoft Windows XP enables administrators and power users to schedule, and, if needed, to script their operations.



MORE INFORMATION
This command-line version of the Disk Defragmenter utility is unable to log information about its operations to the Windows Event log. However, this utility (similar to most console or command-line interface utilities) provides a description of error levels after it has run. If you use these error levels in combination with the Windows Script Host, you can easily generate or log events that this utility may encounter during its operation.

There are a number of ways to obtain, interpret, and view the data that is specific to the utility status information. You can use the ERRORLEVEL value to select a specified path in a Microsoft Visual Basic script.

The Disk Defragmenter utility has the native ability to provide specific &quot;exit code&quot; information to the operating system. The following internal information is related to the Disk Defragmenter utility error levels:

A script can be written to easily monitor and run aspects of the Visual Basic script, based on the ERRORLEVEL value when the utility has completed.

NOTE: Your use of the code provided in this article is at your own risk. Microsoft provides this code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

An example of how to provide event logging for the Disk Defragmenter utility with Windows Script Host is: Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;) Return = WshShell.Run(&quot;defrag %1&quot;, 1, TRUE)

If return = 0 then WshShell.LogEvent 0, &quot;Defrag completed successfully&quot; elseif return = 1 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (defrag was cancelled manually) &quot; elseif return = 2 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (there was a command line error. Check your command line for valid switches and drives)&quot; elseif return = 3 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (there was an unknown error)&quot; elseif return = 4 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (defrag could not run due to insufficient memory resources)&quot; 'errorlevel 5 is not currently used elseif return = 5 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (general error)&quot; elseif return = 6 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (System error: either the account used to run defrag is not an administrator, there is a problem loading the resource DLL, or a defrag engine could not be accessed. Check for proper user permissions and run Sfc.exe to validate system files)&quot; elseif return = 7 then WshShell.LogEvent 1, &quot;Defrag aborted with error level &quot; & return & &quot; (There is not enough free space on the drive. Defrag needs 15% free space to run on a volume)&quot; else WshShell.LogEvent 1, &quot;Defrag aborted with an unknown error level: &quot; & return end if For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

57658 Setting the MS-DOS Errorlevel in a Program

188135 Description of Windows Script Host (WSH)

278411 How to Redirect Command-Line Output

Keywords: kbhowto kbfilesystems kbdefrag KB294743

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.