Detect and Eject CD Drives (VBScript)

AddThis Social Bookmark Button

This script detects, identifies then ejects CD ROM drives on a Windows machine.

Dim ts
 Dim strDriveLetter
 Dim intDriveLetter
 Dim fs 'As Scripting.FileSystemObject
 Const CDROM = 4
     On Error Resume Next
     Set fs = CreateObject("Scripting.FileSystemObject")
     strDriveLetter = ""
     For intDriveLetter = Asc("A") To Asc("Z")
         Err.Clear
         If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then
             If Err.Number = 0 Then
                 strDriveLetter = Chr(intDriveLetter)
                 Exit For
             End If
         End If
     Next

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive WHERE Drive='" & strDriveLetter & ":'")
For Each objItem in colItems
strmsg=""
strmsg=strmsg&"The following disk will be ejected. " &vbcrlf&vbcrlf
strmsg=strmsg&"Description: " & objItem.Description&vbcrlf
strmsg=strmsg&"Drive: " & objItem.Drive&vbcrlf
strmsg=strmsg&"Manufacturer: " & objItem.Manufacturer&vbcrlf
strmsg=strmsg&"Media Loaded: " & objItem.MediaLoaded&vbcrlf

MSGBOX strmsg
Next

 

 

 Set oWMP = CreateObject("WMPlayer.OCX.7" )
 Set colCDROMs = oWMP.cdromCollection
      For d = 0 to colCDROMs.Count - 1
            colCDROMs.Item(d).Eject 
Next 'null
 'A1= msgbox ("Press key To Close Cd" & vbcrlf &_
 ' " The Cd Will Close", 0 + 64, "Open Close Cd Tray")
 '     For d = 0 to colCDROMs.Count  1
 '            colCDROMs.Item(0).Eject
'Next 'null
 
set owmp = nothing
 set colCDROMs = nothing
 wscript.quit

Only members can add comments. Becoming a member is free. See the Members section for more information.