How to provide event logging for the Disk Defragmenter utility with Windows Script Host (294743)
The information in this article 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 "exit code" information to the operating system. The following internal information is related to the Disk Defragmenter utility error levels:
|
Completed, no errors | 0 | User aborted, cancelled | 1 | Bad parameter, syntax error | 2 | Unforeseen, unknown error | 3 | Low memory condition (RAM) | 4 | Not used (undefined), general error | 5 | System, ACL, file mismatch, system wide error | 6 | Low disk space, under 15% free on current volume | 7 |
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 "as is" 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("WScript.Shell")
Return = WshShell.Run("defrag %1", 1, TRUE)
If return = 0 then
WshShell.LogEvent 0, "Defrag completed successfully"
elseif return = 1 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & " (defrag was cancelled manually) "
elseif return = 2 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & " (there was a command line error. Check your command line for valid switches and drives)"
elseif return = 3 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & " (there was an unknown error)"
elseif return = 4 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & " (defrag could not run due to insufficient memory resources)"
'errorlevel 5 is not currently used
elseif return = 5 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & " (general error)"
elseif return = 6 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & "
(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)"
elseif return = 7 then
WshShell.LogEvent 1, "Defrag aborted with error level " & return & " (There is not enough free space on the drive. Defrag needs 15% free space to run on a volume)"
else
WshShell.LogEvent 1, "Defrag aborted with an unknown error level: " & 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
Modification Type: | Minor | Last Reviewed: | 1/15/2006 |
---|
Keywords: | kbDefrag kbFileSystems kbhowto w2000defrag w2000fs KB294743 |
---|
|