1

I need to identify the CD drive and eject the tray. This is executed while booted in WinPE, so the WMP eject function is not available. This script will be used on various computer models/configurations. I'm currently using this:

For Each d in CreateObject("Scripting.FileSystemObject").Drives
    CreateObject("Shell.Application").Namespace(17).ParseName("D:\").InvokeVerb("Eject")
Next

It works, but sometimes it errors and requires user interaction before it ejects. I suspect that it's because of the hardcoded D:\ drive letter, but I could be completely wrong. I need this to work without 3rd party utilities.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
fix
  • 1,425
  • 16
  • 27

2 Answers2

3

Use the DriveType property of the Drive object:

For Each d in CreateObject("Scripting.FileSystemObject").Drives
    WScript.sleep 60
    If d.DriveType = 4 Then
        CreateObject("Shell.Application").Namespace(17).ParseName(d.DriveLetter & ":\").InvokeVerb("Eject")
    End If
Next
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
safetyOtter
  • 1,430
  • 14
  • 26
  • This is very similar to what I've been attempting and your script is getting the same error as mine. Line 3 Char 9, Object Required – fix Jan 08 '13 at 17:36
  • hrmm, maybe add & ":\" after d.DriveLetter – safetyOtter Jan 08 '13 at 17:42
  • Yeah, that eliminated the error, but now the script just doesn't do anything at all. 'For Each d in CreateObject("Scripting.FileSystemObject").Drives if d.DriveType = 4 then drv=d.DriveLetter & ":\" CreateObject("Shell.Application").Namespace(17).ParseName(drv).InvokeVerb("Eject") end if next' – fix Jan 08 '13 at 17:52
  • hrmm, I just tested my edited version and it's popping the cd on my system. Sorry it's not working for you. Good Luck :) – safetyOtter Jan 08 '13 at 17:57
  • Yes, very weird. If I echo everything within the if-statement, everything appears to be good. Frustrating – fix Jan 08 '13 at 18:13
  • 2
    Well, after several hours I got it working by adding a 'WScript.sleep 60' inside the for-loop. This makes it pause 60 milliseconds during each iteration. Maybe it's because I'm in WinPE and it's iterating too fast? No clue, but it works. Thanks for all your help. – fix Jan 08 '13 at 21:21
  • 1
    wow. well, I'm glad you were able to get it working. hope the rest of your project goes more smoothly. – safetyOtter Jan 08 '13 at 22:28
  • I suspect your `Line 3 Char 9, Opject Required` error refers to `WScript.Sleep 60`. If that's the case, you should remove it. – Stephen Quan Jan 09 '13 at 04:41
1

Here is code that uses Media Player to eject; I'm not sure how easy it would be to invoke from your WinPE environment:

' http://www.msfn.org/board/topic/45418-vbscript-for-openingclosing-cd/ 
' http://waxy.org/2003/03/open_cdrom_driv/
Set oWMP = CreateObject("WMPlayer.OCX.7" )
Set colCDROMs = oWMP.cdromCollection
     For d = 0 to colCDROMs.Count - 1
           colCDROMs.Item(d).Eject 
Next 'null

Plan B would be to download a copy of "eject.exe", and include it on your WinPE media:

paulsm4
  • 114,292
  • 17
  • 138
  • 190