windows 批量命令行弹出CD托盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19467792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:00:05  来源:igfitidea点击:

Batch Command Line to Eject CD Tray?

windowsbatch-filecmdbackup

提问by Fawix

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

我目前正在尝试将我的备份 CD 移动到我的备份硬盘上。

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.

为了自动执行任务,我试图创建一个批处理来复制带有 CD 标签的文件,而不是弹出媒体。

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 becauseI want a clear line to draw my attention when the copy finished (I thought opening the tray to be a good one).

我被困在弹出部分。我试图弹出 CD,因为我希望在复制完成时有一条清晰的线条来引起我的注意(我认为打开托盘是一个很好的选择)。

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

任何想法如何使用批处理来做到这一点?或者任何其他方式来“引起注意”复制事件的结束?

Thanks :)

谢谢 :)

回答by npocmaka

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.

这是一个批处理/vbscript 混合(你需要将它保存为一个批处理)。我认为用简单的批处理是不可能做到的。在 Windows 8/8.1 上可能需要下载Windows 媒体播放器(最右边的列) . 某些防病毒程序可能会警告您有关此脚本的信息。

回答by Bruno

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.

只需确保您的 Temp 文件夹中没有名为“temp.vbs”的文件。这可以通过cmd直接执行,你不需要批处理,但我不知道任何像“eject E:\”这样的命令。请记住,这将弹出系统中的所有 CD 托盘。

回答by npocmaka

UPDATE:

更新:

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

一个也支持弹出 U 盘的脚本 - 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 .batextension:

一种不需要 windows 媒体播放器并且不被防病毒程序识别的更好的方法(还)。必须使用.bat扩展名保存:

 @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>

回答by Denis Barmenkov

Requiring administrator's rights is too abusing :)

要求管理员权限太滥用了:)

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

我正在使用 wizmo:https://www.grc.com/WIZMO/WIZMO.HTM