Drive.au3

Last modified:   Thursday, 15 May 2008

#include-once

;==============================================================================
; Function list:
;     _Drive_CD_MediaLoaded()
;     _Drive_DefragVol()
;     _Drive_IsFloppy()
;     _Drive_Optical_Type()
;     
;     
;==============================================================================

;===============================================================================
; Function Name:   _Drive_CD_MediaLoaded()
; Description:   Determine if there is media in a cd or dvd drive
; Syntax:   _Drive_CD_MediaLoaded()
; Parameter(s):   $Drive - Drive to Check
;                           $Computer - Computer on  which the drive is installed (default = localhost)
; Requirement(s):   
; Return Value(s):   Success - Media Loaded Status
;                               Failure - Sets @Error to 1
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s): In the example below, change G: to the actual drive to check
; Example(s):   $L_Status = "Loaded"
;                        If NOT  _Drive_CD_MediaLoaded("G:") Then $L_Status = "Not Loaded"
;                        If @Error Then
;                          MsgBox(0, "Error", "Drive is not a CD drive")
;                        Else
;                          MsgBox(0, "Media Status", "Status = " & $L_Status)
;                        EndIf
;===============================================================================

Func _Drive_CD_MediaLoaded($Drive, $Computer = ".")
   $Drive = StringReplace(StringUpper($Drive), "\", "")
   If StringRight($Drive, 1) <> ":" Then $Drive &= ":"
   If DriveGetType($Drive &"\") <> "CDROM" Then Return SetError(1)
   $Obj_CD = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
   $Items = $Obj_CD.ExecQuery("SELECT * FROM Win32_CDROMDrive WHERE Id = '" & $Drive & Chr(39), "WQL", 0x10 + 0x20)
   If IsObj($Items) then
      For $Item in $Items
         If $Item.MediaLoaded Then Return 1
         Return 0
      Next
   Endif
EndFunc   ;<==> _Drive_CD_MediaLoaded()

;==============================================================================
; Function Name:   _Drive_DefragVol()
; Description:   Defrag a drive volume on Windows Server 2003 or 2008 (NOT for use on XP or Vista)
; Syntax:   _Drive_DefragVol($sVol, $Computer = ".")
; Parameter(s):   $Drive - The drive to check.  This MUST include the colon (eg. a:
;                           $Computer - The network computer to check (Default is localhost)
; Requirement(s):   
; Return Value(s):   Success - Defragments the volume
;                               Failure -  Sets @Error to 1, volume could not be defragmented
; Author(s):   George Gedye (GEOSoft) geog AT mvps DOT org
; Note(s):   (NOT for use on XP or Vista)
;==============================================================================

Func _Drive_DefragVol($sVol, $Computer = ".")
   If (@OSVersion = "WIN_2003" AND @OSBuild = "3790") OR (@OSVersion = "WIN_2008") Then
      $sVol = Chr(39) & StringLeft($sVol, 2) & "\\" 
      $objWMIService = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
      $colVolumes = $objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = " & $sVol &"'")
      For $objVolume in $colVolumes
         $errResult = $objVolume.Defrag()
      Next
      Return $errResult
   EndIf
   Return SetError(1)
EndFunc   ;<==> _Drive_DefragVol()

;==============================================================================
; Function Name:   _Drive_IsFloppy()
; Description:   Determines if a drive is a floppy drive
; Syntax:   _Drive_IsFloppy($Drive, $Computer = ".")
; Parameter(s):   $Drive - The drive to check.  This MUST include the colon (eg. a:
;                           $Computer - The network computer to check (Default is localhost)
; Requirement(s):   
; Return Value(s):   1 - $Drive is a Floppy drive,  0- $Drive is not a Floppy drive
; Author(s):   George Gedye (GEOSoft) goeg AT mvps DOT org
;==============================================================================

Func _Drive_IsFloppy($Drive, $Computer = ".")
   $Drive = StringReplace($Drive, "\", "")
   Local $colItems = ""
   $objWMIService = ObjGet("winmgmts:\\" & $Computer & "\root\CIMV2")
   $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE MediaType = '5'", "WQL", 0x30)
   If IsObj($colItems) then
      For $objItem In $colItems
         If $objItem.DeviceID = $Drive Then
            Return 1
         Else
            Return 0
         EndIf
      Next
   Endif
EndFunc   ;<==> _Drive_IsFloppy()

;==============================================================================
; Function Name:   _Drive_Optical_Type()
; Description:   Retrieve the type of drive (CD, DVD, Virtual &etc.)
; Syntax:   _Drive_Optical_Type( $Drive, [$Computer] )
; Parameter(s):   $Drive - The drive to check.  This MUST include the colon (eg. a:
;                           $Computer - The network computer to check (Default is localhost)
; Requirement(s):   WMI capable system
; Return Value(s):   Success - Returns the drive type
; Author(s):   George Gedye (GEOSoft) geog AT mvps DOT org
; Example(s):
;     $Drives = DriveGetDrive("CDROM")
;
;     For $I = 1 To Ubound($Drives) -1
;        MsgBox(0, "Test", "Drive " & StringUpper($Drives[$I]) & " is a  " & _Drive_Optical_Type($Drives[$I]))
;     Next
;==============================================================================

Func _Drive_Optical_Type( $Drive, $Computer = ".")
   If DriveGetType($Drive) <> "CDROM" Then Return SetError(1)
   Local $oRtn = ""
   $Drive = StringUpper($Drive)
   $Obj_CD = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
   $Items = $Obj_CD.ExecQuery("Select * from Win32_CDROMDrive WHERE Id = '" & $Drive & Chr(39))
   
   For $Item in $Items
      $oRtn = StringMid($Item.DeviceID, StringInStr($Item.DeviceID, "_")+1)
      If StringInStr($oRtn, "Virtual") Then
         $oRtn = StringMid($oRtn, StringInStr($oRtn, "Virtual"))
         $oRtn = StringReplace($oRtn, "_", " ", 1)
         $oRtn = StringLeft($oRtn, 15)
      Else
         $oRtn = StringLeft($oRtn, StringInStr($oRtn, "_") -1)
      EndIf
   Next
   If $oRtn = "" Then Return SetError(1)
   Return $oRtn
EndFunc   ;<==> _Drive_Optical_Type()