Batch Command Line to Eject CD Tray?

42,167

Solution 1

if you have no installed media player or anti virus alarms check my other answer.

:sub echo(str) :end sub
echo off
'>nul 2>&1|| copy /Y %windir%\System32\doskey.exe '.exe >nul

'& cls
'& cscript /nologo /E:vbscript %~f0
'& pause




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

if colCDROMs.Count >= 1 then
        For i = 0 to colCDROMs.Count - 1
                colCDROMs.Item(i).Eject
        Next ' cdrom
End If

This is a batch/vbscript hybrid (you need to save it as a batch) .I don't think is possible to do this with simple batch.On windows 8/8.1 might require download of windows media player (the most right column).Some anti-virus programs could warn you about this script.

Solution 2

I know this question is old, but I wanted to share this:

@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

just make sure you don't have a file called "temp.vbs" in your Temp folder. This can be executed directly through a cmd, you don't need a batch, but I don't know any command like "eject E:\". Remember that this will eject all CD trays in your system.

Solution 3

UPDATE:

A script that supports also ejection of a usb sticks - ejectjs.bat:

::to eject specific dive by letter
call ejectjs.bat G
::to eject all drives that can be ejected
call ejectjs.bat *

A much better way that does not require windows media player and is not recognized by anti-virus programs (yet) .Must be saves 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>

Solution 4

Requiring administrator's rights is too abusing :)

I am using wizmo: https://www.grc.com/WIZMO/WIZMO.HTM

Share:
42,167
Fawix
Author by

Fawix

Knowledge is the power to control one's own life That is my motto; thus I like to learn, I like to read books and manga, watch anime play video-games and develop solutions! To learn more about my technical profile feel free to visit my LinkedIn!

Updated on April 02, 2020

Comments

  • Fawix
    Fawix about 4 years

    I'm currently trying to move my CD's of backup to my Backup HDD.

    To automate the task I'm trying to create a batch to copy the files with the label of the CD than eject the media.

    The code looks like this so far:

    @echo off 
    SET dest=F:\Backup\
    
    d:
    
    :: routine to retrieve volume label.
    for /f "tokens=1-5*" %%1 in ('vol') do (
       set vol=%%6 & goto done
    )
    :done
    
    :: create destination folder
    set dest=%dest%%vol%
    mkdir "%dest%"
    
    :: copy to destiny folder
    xcopy "d:" "%dest%" /i /s /exclude:c:\excludes.txt
    
    ::eject CD
    
    
    
    c:
    

    I'm stuck at eject part. I'm trying to eject the CD because I want a clear line to draw my attention when the copy finished (I thought opening the tray to be a good one).

    Any ideas how to do it using Batch? Or any other ways to "draw the attention" to the end of the copy event?

    Thanks :)

  • Fawix
    Fawix over 10 years
    that is quite cool! I didn't know you could make hybrid scripts. I'll dig further into that! it works wonders! thanks! :)
  • Tqn
    Tqn almost 10 years
    AVG is like: "This is a threat!" Great job though. :)