# Eject/Load DVDROM device from Command Line



## saivstech (Dec 5, 2007)

hello all,

I'd like to know if there are any specific commands to open/eject and load/close a CD/DVD device from command prompt.

I dont want to use any utilities/downloads for the above purpose.

saivstech


----------



## valis (Sep 24, 2004)

pretty certain it's not a native command in XP. May have been in original dos, but I doubt it, as CD's weren't too popular then. Pretty sure you can write a batch file for it, though.


----------



## saivstech (Dec 5, 2007)

valis said:


> pretty certain it's not a native command in XP. May have been in original dos, but I doubt it, as CD's weren't too popular then. Pretty sure you can write a batch file for it, though.


really appreciate the effort . Anyways, any suggestions about how to write a batch file??


----------



## valis (Sep 24, 2004)

writing batch files is easy. Use notebook and safe it as .bat instead of .txt. You can probably google for a .bat file that will open/close your cd drive.

Just out of morbid curiousity, why are you wanting to do this? To me, and this is just the engineer in me asking, it's sort of like having an 'eject' button on your dvd player or vcr. You still have to go get the tape.

So, spill. Why?


----------



## saivstech (Dec 5, 2007)

valis said:


> writing batch files is easy. Use notebook and safe it as .bat instead of .txt. You can probably google for a .bat file that will open/close your cd drive.
> 
> Just out of morbid curiousity, why are you wanting to do this? To me, and this is just the engineer in me asking, it's sort of like having an 'eject' button on your dvd player or vcr. You still have to go get the tape.
> 
> So, spill. Why?


been googling since this morning but wasnt lucky so far.

lemme clarify ur curiosity. i had been having some probs with my drive eject/close button, sometimes it works and at times doesnt. This invoked the engineer in me to find out some alternatives to this evolving problem, hence the curiosity . Hope am clear.


----------



## valis (Sep 24, 2004)

try here.

think it's called fxeject or some such.


----------



## saivstech (Dec 5, 2007)

thanx, as I said i'm looking for some thing like using the batch files instead of some utilities. lets me have a control of what i'm dealing with.


----------



## linkinworm (Dec 10, 2007)

try this, type this into notepad and save it as EjectCD.vbs then copy the file into the C:\ root folder
(note change the letter E to your drive letter, usualy D)

Set objShell = CreateObject("Shell.Application")
Set MyComp=objShell.NameSpace(17)
Set CD=MyComp.ParseName("E:")
Set Context=CD.Verbs
CD.InvokeVerb "Eject" 

then from CMD type in 
start C:\EjectCD.vbs

EDIT SORRY wrong commad for the file if just copy it again if u have tried it, its fixed now. 
also forgot to add a load command

Set objShell = CreateObject("Shell.Application")
Set MyComp=objShell.NameSpace(17)
Set CD=MyComp.ParseName("E:")
Set Context=CD.Verbs
CD.InvokeVerb "Load" 

just save this as LoadCD.vbs and then copy it to C:\ 
CMD line 
start LoadCD.vbs

HOPE THIS HELPS 

PS if in vista u might have to use CMD in administrator to get this to work, just go to start ,all programs, accessories, cmd, right click on it then run as administrator.


EDIT 2

ok for an easy access icon go to notepad and add 

start EjectCD.vbs
cmd

then save as EjectCD.bat

and for load 

start LoadCD.vbs
cmd

save as LoadCD.bat

its pretty long but it solves your problem i think


----------



## saivstech (Dec 5, 2007)

thanks very much. Only EJECT is working, and LOAD/CLOSE is not working.


----------



## linkinworm (Dec 10, 2007)

i havnt be able to try the load/close option as im useing a laptop and not a desktop, have u tried replacing the "load" with Close? tell us if that works

EDIT, at a more closer look at the script, it would seem u cant load the cd drive because windows is just stupid that way. cant u just gently push the drive in when u need to? also whats wrong with the close button?
ill try find a way to close it but for now u might have to live with it, with out the use of other programs

is this for school or something? have they locked the cd drives?, if so, i think they might of just turned them off in device manager, and this wont work for that, but if u know how to use cmd u might be able to turn it back on, make a cmd.bat then run it, 
to make a cmd.bat open notpad then type in command.com then save as command.bat, open it , let it run a bit(my school comps showed other text for cmd, never read it i think it was just the pc specs or something to do with the server) then just add your self as and admin then open device manager and enable the cd drive, then delete the admin account, dont get caught tho, u could get kicked out.


----------



## saivstech (Dec 5, 2007)

allright i've tried to replace "load" with "close" and it doesnt seem to be working either. 

i'll definitely be willing to push the button when needed. but there has been a slight glitch with the button, as it gets jammed sometimes and doesnt work. Hence this entire effort to eject/load thru CLI, as I dont want to get stranded in the future with such probs. Hope to find a way too. 

Its not for school, infact this is my home pc. jfyi.


----------



## saivstech (Dec 5, 2007)

I found these links but wasnt sure which way to go .....I thought of sharing this info. for all the help extended to me...

1. http://forums.pcpitstop.com/lofiversion/index.php/t143721.html

2. http://www.virtualplastic.net/msgboard/thread.php?forum=0&thread=158

3. This following one I downloaded from somewhere over the net, cant remember though where I got this code from??? Me think, its written in C++.

Thought it should shed some light on what I asked for, CLOSING the drive tray. FYI.
------------------------------------------------------------------------------------------------------------------------
// 
// AUTHOR
// N. Nielsen
//
// LICENSE
// This software is in the public domain.
//
// The software is provided "as is", without warranty of any kind,
// express or implied, including but not limited to the warranties
// of merchantability, fitness for a particular purpose, and
// noninfringement. In no event shall the author(s) be liable for any
// claim, damages, or other liability, whether in an action of
// contract, tort, or otherwise, arising from, out of, or in connection
// with the software or the use or other dealings in the software.
// 
// SUPPORT
// Send bug reports to: <[email protected]>
//

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <tchar.h>
#include <winioctl.h>

// Get first CD-ROM Drive letter
TCHAR GetCDROM()
{
TCHAR buff[4];
TCHAR i;

_tcscpy(buff, "X:\\");

for(i = _T('A'); i <= _T('Z'); i++)
{
buff[0] = i;
if(GetDriveType(buff) == DRIVE_CDROM)
return i;
}

return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

TCHAR buff[7];
HANDLE hDevice;

// Create a CD-ROM drive string
_tcscpy(buff, "\\\\.\\X:");
buff[4] = GetCDROM();

if(buff[4] == 0)
{
MessageBox(NULL, _T("No CD-ROM drives found on system."), _T("Eject"), 
MB_OK | MB_ICONSTOP);
return 2;
}

// Open the CD device
hDevice = CreateFile(buff, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 
NULL, OPEN_EXISTING, 0, NULL);

if(hDevice != INVALID_HANDLE_VALUE)
{
DWORD code = IOCTL_STORAGE_EJECT_MEDIA;
DWORD bytesRet = 0;

// If specified with the '-load' parameter then load 
// the drive rather than eject
if(strstr(lpCmdLine, "-load"))
code = IOCTL_STORAGE_LOAD_MEDIA;

DeviceIoControl(hDevice, code, NULL, 0, NULL, 0, &bytesRet, NULL);
}

return 0;
}
------------------------------------------------------------------------------------------------------------------------


----------



## linkinworm (Dec 10, 2007)

i tired to build the program but i kept getting an error, i havnt read through it to correct where needed, cos its almost 6am and im really tired, if u want i can try make the close program for you in about 12 14 hours when i wake up cos im going sleep now, it wont be in a cmd command, but an .exe file

why not just use a program to do it, they run in the tool tray and when u right click it u get the option to open and close the cd tray, its much easier

www.rjlsoftware.com/software/utility/open_cd

thats off one of those links


----------



## saivstech (Dec 5, 2007)

goodnight and get some good days rest b4 we get online. thanks for the support stayin awake so late. appreciate that.

thanks pal once again. would really appreciate if u can help me with that exe. sametime i'd like to know how to make an exe and the code, so that I can learn making others on the same lines for me.

http://www.programmersheaven.com/download/3137/download.aspx

http://techrepublic.com.com/5208-6247-0.html?forumID=12&threadID=85326&messageID=518360


----------



## Elvandil (Aug 1, 2003)

cdrecord.exe is easy to use in a batch file. For example, to open my first CD-ROM:

cdrecord.exe dev=2,0,0 -eject

To close it:

cdrecord.exe dev=2,0,0 -load

To find the parameters for your drives:

cdrecord.exe -scanbus

I attached the file for you. It's a fantastic little exec that uses only 3 libraries: ntdll.dll, kernel32.dll, and msvcrt.dll.


----------



## saivstech (Dec 5, 2007)

Elvandil said:


> cdrecord.exe is easy to use in a batch file. For example, to open my first CD-ROM:
> 
> cdrecord.exe dev=2,0,0 -eject
> 
> ...


thanx pal. will look into it, at the moment still trying to figure out using vbscript.


----------

