25

I have been trying to eject the cd drive with the use of the cmd.
However, I am stumped. Searching on the internet, I only found this answer:

eject D: 

and a similiar answer

eject D: /I

Both of them do not work.

EDIT
Now people have found this answer on superuser,

Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
colCDROMs.Item(0).Eject

However, I get an error:

colCDROMS.Item is not recognized as an internal or external command, operable program or batch file.

migrated from stackoverflow.com Sep 13 '15 at 15:03

This question came from our site for professional and enthusiast programmers.

  • Im running windows 7, so batch files from xp may not work. – Penguinz Sep 13 '15 at 14:31
  • I'm asking a Meta question on this since I'm confused as well, and the tag makes no mention of when batch-file questions become off-topic. – AStopher Sep 13 '15 at 14:39
  • The xp version is giving me problems. – Penguinz Sep 13 '15 at 14:47
  • On Meta: When are 'batch-file' questions off-topic?. – AStopher Sep 13 '15 at 14:47
  • 2
    That's definitely not Batch-file scripting; it's VBS – Canadian Luke Sep 14 '15 at 16:33
20

You can eject a cd with a batch file (this is part vbscript

@echo off
echo Set oWMP = CreateObject("WMPlayer.OCX.7")  >> %temp%\temp.vbs
echo Set colCDROMs = oWMP.cdromCollection       >> %temp%\temp.vbs
echo For i = 0 to colCDROMs.Count-1             >> %temp%\temp.vbs
echo colCDROMs.Item(i).Eject                    >> %temp%\temp.vbs
echo next                                       >> %temp%\temp.vbs
echo oWMP.close                                 >> %temp%\temp.vbs
%temp%\temp.vbs
timeout /t 1
del %temp%\temp.vbs

This is not my work, I found it in the stackoverflow community:

Post Link: Batch Command Line to Eject CD Tray?
Answer Author: Bruno
Date Answered: Feb 10, 2015

  • 14
    You need to state where you got it from: This is not my work, I found it online.. If you can't remember, you need to delete your answer for copyright reasons. – AStopher Sep 13 '15 at 14:34
  • This answer is being discussed on Meta: How do we deal with answers that do not attribute? – AStopher Sep 13 '15 at 15:00
  • 6
    Technically speaking, this is a VBS solution, not a batch file solution. – Dan Henderson Sep 13 '15 at 23:13
  • 4
    To clarify my previous comment, I don't mean to say that this isn't an appropriate answer for the question as stated, merely that perhaps the opening line "You can eject a cd with a batch file" should be modified slightly. If my system's configuration supports the execution of batch files but not of VB scripts, this answer will not work for me. – Dan Henderson Sep 14 '15 at 0:30
  • As of Dec 2016, Windows Defender on Windows 10 will detect the generated script as a virus when run – hooby3dfx Dec 22 '16 at 2:09
18

You could use the Shell.Application COM object's InvokeVerb method. From a cmd prompt, you can abuse a PowerShell one-liner thusly:

powershell "(new-object -COM Shell.Application).NameSpace(17).ParseName('D:').InvokeVerb('Eject')"

You can also use Windows Scripting Host (VBScript / JScript) to invoke the COM object. Here's an example using a hybrid Batch + Jscript script (save it with a .bat extension):

@if (@CodeSection == @Batch) @then

@echo off
setlocal

set "CDdrive=D:"

cscript /nologo /e:JScript "%~f0" "%CDdrive%"

goto :EOF

@end // end batch / begin JScript hybrid chimera
var oSH = WSH.CreateObject('Shell.Application');
oSH.NameSpace(17).ParseName(WSH.Arguments(0)).InvokeVerb('Eject');

If you prefer to have your script detect the drive letter for the CD drive, that can be arranged as well. Here's a more complete version with comments explaining some of the non-self-explanatory values.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

cscript /nologo /e:JScript "%~f0"

goto :EOF

@end // end batch / begin JScript hybrid chimera

// DriveType=4 means CD drive for a WScript FSO object.
// See http://msdn.microsoft.com/en-us/library/ys4ctaz0%28v=vs.84%29.aspx

// NameSpace(17) = ssfDRIVES, or My Computer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096%28v=vs.85%29.aspx

var oSH = new ActiveXObject('Shell.Application'),
    FSO = new ActiveXObject('Scripting.FileSystemObject'),
    CDdriveType = 4,
    ssfDRIVES = 17,
    drives = new Enumerator(FSO.Drives);

while (!drives.atEnd()) {
    var x = drives.item();
    if (x.DriveType == CDdriveType) {
        oSH.NameSpace(ssfDRIVES).ParseName(x.DriveLetter + ':').InvokeVerb('Eject');
        while (x.IsReady)
            WSH.Sleep(50);
    }
    drives.moveNext();
}
4

Command line CD-eject oneliner:

In a bat file or directly in cmd this worked after first run of wmplayer on Windows 8:

powershell (New-Object -com "WMPlayer.OCX.7").cdromcollection.item(0).eject()
4

Using WMPlayer.OCX.7 will startled most of the anti-virus programs and there are some versions of windows that come without media player. Here's a way with shell.application and invokeVerb function:

save this with .bat extension:

@cScript.EXE //noLogo "%~f0?.WSF"  //job:info %~nx0 %*
@exit /b 0

   <job id="info">
      <script language="VBScript">
        if WScript.Arguments.Count < 2 then
            WScript.Echo "No drive letter passed"
            WScript.Echo "Usage: " 
            WScript.Echo "  " & WScript.Arguments.Item(0) & " {LETTER|*}"
            WScript.Echo "  * will eject all cd drives"
            WScript.Quit 1
        end if
        driveletter = WScript.Arguments.Item(1):
        driveletter = mid(driveletter,1,1):

        Public Function ejectDrive (drvLtr)
            Set objApp = CreateObject( "Shell.Application" ):
            Set objF=objApp.NameSpace(&H11&):
            'WScript.Echo(objF.Items().Count):
            set MyComp = objF.Items():
            for each item in objF.Items() :
                iName = objF.GetDetailsOf (item,0): 
                iType = objF.GetDetailsOf (item,1): 
                iLabels = split (iName , "(" ) :
                iLabel = iLabels(1):

                if Ucase(drvLtr & ":)") = iLabel and iType = "CD Drive" then
                    set verbs=item.Verbs():
                    set verb=verbs.Item(verbs.Count-4):
                    verb.DoIt():
                    item.InvokeVerb replace(verb,"&","") :
                    ejectDrive = 1:
                    exit function:

                end if
            next    
            ejectDrive = 2:
        End Function

        Public Function ejectAll ()
            Set objApp = CreateObject( "Shell.Application" ):

            Set objF=objApp.NameSpace(&H11&):
            'WScript.Echo(objF.Items().Count):
            set MyComp = objF.Items():
            for each item in objF.Items() :
                iType = objF.GetDetailsOf (item,1):                                 
                if  iType = "CD Drive" then
                    set verbs=item.Verbs():
                    set verb=verbs.Item(verbs.Count-4):
                    verb.DoIt():
                    item.InvokeVerb replace(verb,"&","") :
                end if

            next
        End Function
        if driveletter = "*" then 
            call ejectAll
            WScript.Quit 0
        end if
        result = ejectDrive (driveletter):

        if result = 2 then
            WScript.Echo "no cd drive found with letter " & driveletter & ":"
            WScript.Quit 2
        end if

      </script>
  </job>

You can use it like (for more info -)

call eject.bat *
  • Even with eject.bat * it tells me no cd drive found with letter d:. Why might this be? If I right click the drive and choose "Eject", it ejects. – Brad Turek Feb 26 at 4:11
  • @BradTurek - it could be the language (if it is not English) . Or if you have custom actions associated with the right click over cd drives. If you right-click on the CD/DVD device eject action should the 4th from the bottom (the default place). – npocmaka Feb 26 at 7:18
0

If you can use a third-party application, you can use Nirsoft's nircmd. On all the PCs I tried (from Windows XP to Windows 8), I could eject the disc using:

"C:\path_to\nircmd.exe" cdrom open X:

Where X is your disc drive letter.

0

In Windows 10 I use this little script. It works!

dim oWMP
  Set oWMP = CreateObject("WMPlayer.OCX.7")
  Set colCDROMs = oWMP.cdromCollection
  colCDROMs.Item(0).Eject
  set oWMP = nothing
  • Please read the question again carefully. Your answer does not answer the original question. Op is using Windows 7 and he has already tried you script and said it didn't work. – DavidPostill Oct 5 '16 at 8:36
  • is this script for cmd, powershell or smth else? – YakovL Apr 25 '17 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.