Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ejectcd.vbs

(view source code of ejectcd.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim blnFoundCDROM, objCDROMDrive, objDrive, objFSO, objWMP, strCDROM, strMsg
  4.  
  5. On Error Resume Next
  6.  
  7. If WScript.Arguments.Named.Count <> 0 Then Syntax
  8.  
  9. If WScript.Arguments.Unnamed.Count = 1 Then
  10. 	strCDROM = UCase( WScript.Arguments.Unnamed( 0 ) )
  11. 	If Len( strCDROM ) > 2 Then
  12. 		strMsg = "Please specify a CDROM drive to eject."
  13. 		Syntax
  14. 	End If
  15. 	If InStr( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", Left( strCDROM, 1 ) ) < 1 Then
  16. 		strMsg = "Please specify a CDROM drive to eject."
  17. 		Syntax
  18. 	End If
  19. 	If Len( strCDROM ) = 2 And Right( strCDROM, 1 ) <> ":" Then
  20. 		strMsg = "Please specify a CDROM drive to eject."
  21. 		Syntax
  22. 	End If
  23. 	blnFoundCDROM = False
  24. 	Set objFSO = WScript.CreateObject( "Scripting.FileSystemObject" )
  25. 	For Each objDrive In objFSO.Drives
  26. 		If ( objDrive.DriveLetter = strCDROM Or objDrive.Path = strCDROM ) And objDrive.DriveType = 4 Then
  27. 			blnFoundCDROM = True
  28. 		End If
  29. 	Next
  30. 	If blnFoundCDROM = False Then
  31. 		strMsg = "Drive " & Left( strCDROM, 1 ) & ": is not a CDROM drive."
  32. 		Syntax
  33. 	End If
  34. Else
  35. 	strMsg = "Please specify a CDROM drive to eject."
  36. 	Syntax
  37. End If
  38.  
  39. Set objWMP = CreateObject( "WMPlayer.OCX.7" )
  40. If Err Then
  41. 	strMsg = "This script requires Windows Media Player."
  42. 	Syntax
  43. End If
  44.  
  45. Set objCDROMDrive = objWMP.cdromCollection.getByDriveSpecifier( strCDROM )
  46. If Err Then
  47. 	strMsg = "CDROM drive " & strCDROM & " not accessible"
  48. 	Syntax
  49. End If
  50.  
  51. objCDROMDrive.Eject
  52. If Err Then
  53. 	strMsg = "Error ejecting CDROM drive " & strCDROM
  54. 	Syntax
  55. End If
  56.  
  57. Sub Syntax
  58. 	strMsg = strMsg                                                  & vbCrLf & vbCrLf & vbCrLf _
  59. 	       & "EjectCD.vbs,  Version 1.00"                                              & vbCrLf _
  60. 	       & "Eject a CD-ROM based on a specified drive letter"               & vbCrLf & vbCrLf _
  61. 	       & "Usage:  CSCRIPT  EJECTCD.VBS  d:"                               & vbCrLf & vbCrLf _
  62. 	       & "Where:  ""d:"" is the drive letter of the CDROM to be ejected"  & vbCrLf & vbCrLf _
  63. 	       & "This script requires Windows Media Player"                      & vbCrLf & vbCrLf _
  64. 	       & "Written by Rob van der Woude"                                            & vbCrLf _
  65. 	       & "http://www.robvanderwoude.com"                                           & vbCrLf
  66. 	WScript.Echo strMsg
  67. 	WScript.Quit( 1 )
  68. End Sub
  69.  

page last modified: 2024-04-16; loaded in 0.0073 seconds