Open/close the CD drive with a bat

Solved
freeze -  
 LIAMSI3456 -
Hello,
what is the command that allows you to open/close the CD drive in a batch file or another language supported by Windows (since I don't have any compilers)
thank you
Configuration: Windows XP Internet Explorer 6.0

24 answers

  • 1
  • 2
  1. thib0787 Posted messages 934 Status Member 176
     
    Hi, I found something,

    you can launch a .vbs from your bat to open the drive (not close it)

    here's the vbs

    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
    11
    1. Selim
       
      Thank you
      0
  2. hamza_0081 Posted messages 260 Status Member 60
     
    Hello to open and then close your drive but if it is already open it closes it...
    I found this VBS tutorial

    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
    colCDROMs.Item(d).Eject
    End If
    msgbox "hamza your cd rom is closed"

    and on cmd I found this

    echo off && Cls && Mode 55,5 && Color 4f && Title Open Cd
    Set VBS=%systemdrive%\OpenClose.vbs
    Set VBS1=%systemdrive%\RemoveVBS.vbs
    echo Preparing To Run The Script!
    >> %vbs% Echo Dim ts
    >> %vbs% Echo Dim strDriveLetter
    >> %vbs% Echo Dim intDriveLetter
    >> %vbs% Echo Dim fs 'As Scripting.FileSystemObject
    >> %vbs% Echo Const CDROM = 4
    >> %vbs% Echo On Error Resume Next
    >> %vbs% Echo Set fs = CreateObject("Scripting.FileSystemObject")
    >> %vbs% Echo strDriveLetter = ""
    >> %vbs% Echo For intDriveLetter = Asc("D") To Asc("Z")
    >> %vbs% Echo Err.Clear
    >> %vbs% Echo If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then
    >> %vbs% Echo If Err.Number = 0 Then
    >> %vbs% Echo strDriveLetter = Chr(intDriveLetter)
    >> %vbs% Echo Exit For
    >> %vbs% Echo End If
    >> %vbs% Echo End If
    >> %vbs% Echo Next
    >> %vbs% Echo MSGBOX "YOUR CD IS: " ^& strDriveLetter,0 + 48,"CDROM=> " ^& strDriveLetter
    >> %vbs% Echo Set oWMP = CreateObject("WMPlayer.OCX.7" )
    >> %vbs% Echo Set colCDROMs = oWMP.cdromCollection
    >> %vbs% Echo colCDROMs.Item(d).Eject
    >> %vbs% Echo A1= msgbox ("Press key To Close Cd" ^& vbcrlf ^& "The Cd Will Close", 0 + 64, "Open Close Cd Tray")
    >> %vbs% Echo colCDROMs.Item(d).Eject
    >> %vbs% Echo set owmp = nothing
    >> %vbs% Echo set colCDROMs = nothing
    >> %vbs% Echo wscript.quit

    start %vbs%
    ping -n 12 127.0.0.1>nul
    TASKKILL /F /T /IM Wscript.exe
    TASKKILL /F /T /IM Wmiprvse.exe

    >> %vbs1% Echo On Error Resume Next
    >> %vbs1% Echo Dim ACT : Set ACT = CreateObject("WScript.Shell")
    >> %vbs1% Echo Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
    >> %vbs1% Echo Loc = ACT.ExpandEnvironmentStrings("%SystemDrive%")
    >> %vbs1% Echo Fso.Deletefile("%VBS%")
    >> %vbs1% Echo Fso.Deletefile(Loc ^& "\CD-Open-Close.cmd")
    >> %vbs1% Echo Fso.Deletefile("%VBS1%")
    >> %vbs1% Echo Set ACT = Nothing
    >> %vbs1% Echo Set Fso = Nothing
    start /w %vbs1%
    exit

    we can use the same dos method with the first script I will test it and if it works I will send you the code

    @+
    5
    1. Dean76
       
      In VBS it works great, thank you!

      But in batch (cmd) with Windows Vista it doesn't work.
      It shows me "Access Denied." and Windows cannot find 'I:\OpenClose.vbs.'

      Can someone help me?
      0
    2. hamza_0081 Posted messages 260 Status Member 60 > Dean76
       
      Good evening
      I am not an expert and I also copied this script from another forum [sorry, I don't remember the link]


      you can simply delete it if the start command is in the line
      start %vbs% and the script will be:





      echo off && Cls && Mode 55,5 && Color 4f && Title Open Cd
      Set VBS=%systemdrive%\OpenClose.vbs
      Set VBS1=%systemdrive%\RemoveVBS.vbs
      echo Preparing To Run The Script!
      >> %vbs% Echo Dim ts
      >> %vbs% Echo Dim strDriveLetter
      >> %vbs% Echo Dim intDriveLetter
      >> %vbs% Echo Dim fs 'As Scripting.FileSystemObject
      >> %vbs% Echo Const CDROM = 4
      >> %vbs% Echo On Error Resume Next
      >> %vbs% Echo Set fs = CreateObject("Scripting.FileSystemObject")
      >> %vbs% Echo strDriveLetter = ""
      >> %vbs% Echo For intDriveLetter = Asc("D") To Asc("Z")
      >> %vbs% Echo Err.Clear
      >> %vbs% Echo If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then
      >> %vbs% Echo If Err.Number = 0 Then
      >> %vbs% Echo strDriveLetter = Chr(intDriveLetter)
      >> %vbs% Echo Exit For
      >> %vbs% Echo End If
      >> %vbs% Echo End If
      >> %vbs% Echo Next
      >> %vbs% Echo MSGBOX "YOUR CD IS: " ^& strDriveLetter,0 + 48,"CDROM=> " ^& strDriveLetter
      >> %vbs% Echo Set oWMP = CreateObject("WMPlayer.OCX.7" )
      >> %vbs% Echo Set colCDROMs = oWMP.cdromCollection
      >> %vbs% Echo colCDROMs.Item(d).Eject
      >> %vbs% Echo A1= msgbox ("Press key To Close Cd" ^& vbcrlf ^& " The Cd Will Close", 0 + 64, "Open Close Cd Tray")
      >> %vbs% Echo colCDROMs.Item(d).Eject
      >> %vbs% Echo set owmp = nothing
      >> %vbs% Echo set colCDROMs = nothing
      >> %vbs% Echo wscript.quit

      start %vbs%
      ping -n 12 127.0.0.1>nul
      TASKKILL /F /T /IM Wscript.exe
      TASKKILL /F /T /IM Wmiprvse.exe

      >> %vbs1% Echo On Error Resume Next
      >> %vbs1% Echo Dim ACT : Set ACT = CreateObject("WScript.Shell")
      >> %vbs1% Echo Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
      >> %vbs1% Echo Loc = ACT.ExpandEnvironmentStrings("%SystemDrive%")
      >> %vbs1% Echo Fso.Deletefile("%VBS%")
      >> %vbs1% Echo Fso.Deletefile(Loc ^& "\CD-Open-Close.cmd")
      >> %vbs1% Echo Fso.Deletefile("%VBS1%")
      >> %vbs1% Echo Set ACT = Nothing
      >> %vbs1% Echo Set Fso = Nothing
      %vbs1% 'here the line that has been modified
      exit





      at first glance your system [os] is installed on volume I:\ usually Windows is installed on volume C:\
      the two following instructions are used to create two variables vbs and vbs1

      Set VBS=%systemdrive%\OpenClose.vbs
      Set VBS1=%systemdrive%\RemoveVBS.vbs

      so in your case the variables vbs and vbs1 have the following values

      vbs=I:\OpenClose.vbs
      vbs1=I:\RemoveVBS.vbs

      the following line: start %vbs%
      calls the file whose name is held in the variable vbs and as vbs=I:\OpenClose.vbs
      it executes the file (OpenClose.vbs)

      since your system cannot find the file that should be created on I: try replacing the lines

      Set VBS=%systemdrive%\OpenClose.vbs
      Set VBS1=%systemdrive%\RemoveVBS.vbs

      with

      Set VBS=I:\OpenClose.vbs
      Set VBS1=I:\RemoveVBS.vbs

      it may be that the variable defined by default %systemRoot% is not present in the DOS version of Vista [I'm not sure]

      another thing you can simply delete if the start command is in the line
      start %vbs%


      good luck I hope it works for you if not, I am at your service :-)


      finally I can offer you a simpler script in DOS [a batch that generates a VBS script :-)]



      @echo off&&color 0a&& title Opening CD-ROM drive&& mode 55,5
      set vbs=c:\openCD.vbs 'or I:\openCD.vbs
      echo opening the drawer ...
      >>%vbs% echo Set oWMP = CreateObject("WMPlayer.OCX.7" )
      >>%vbs% echo Set colCDROMs = oWMP.cdromCollection
      >>%vbs% echo if colCDROMs.Count >= 1 then
      >>%vbs% echo For i = 0 to colCDROMs.Count - 1
      >>%vbs% echo colCDROMs.Item(i).Eject
      >>%vbs% echo Next ' cdrom
      >>%vbs% echo colCDROMs.Item(d).Eject
      >>%vbs% echo msgbox " Dean76 your cd rom is closed"
      %vbs%
      del %vbs%




      you can change the volume I: to another of your choice!
      0
    3. questionneur
       
      Hi Hamza;
      In your script, I see "ping -n 12 127.0.0.1>nul" does that start the script to open and close the drive or is it an error?
      Best regards,
      0
    4. nea
       
      127.0.0.1 is a local ping of the computer; apparently, we want to ping our own PC basically... I'm too lazy to read all the code, but there you go...
      0
    5. Gagaouf
       
      Not bad at all the script.
      0
  3. freeze
     
    the mechanical opening and closing of the reader (ejection) that makes the sound dzzzzrrrrrrrrrrrrrrrt
    4
  4. hamza_0081 Posted messages 260 Status Member 60
     
    Salut, do we need a compiler to run your VBS on XP?
    3
  5. arrial Posted messages 1803 Registration date   Status Member Last intervention   251
     
    Sure. VBScript is an uncompiled language, just like you didn't have to compile QuickBasic to run it. It runs under Windows Scripting Host, a script interpreter for Microsoft Windows systems, so under Windows ‼
    ▼▼▼
    https://www.commentcamarche.net/contents/1187-introduction-a-vbscript
    ▲▲▲

    Tip worth knowing: I'm going to stash this in my archives…

    @+
    (Did you notice that your messages are posted twice?)
    3
  6. hamza_0081 Posted messages 260 Status Member 60
     
    Good evening, I know the problem is solved but I would like to add a little cool trick, it's the >>nircmd<<
    you can do plenty of things like opening the drawer of your choice, making a window transparent or invisible, hiding the desktop, the request button !!!!!! even turning off the monitor !!!
    take a screenshot !!!!! and many other things :-] awesome, isn't it??
    3
  7. arrial Posted messages 1803 Registration date   Status Member Last intervention   251
     
    What do you mean by open/close?
    2
  8. arrial Posted messages 1803 Registration date   Status Member Last intervention   251
     
    I doubt that cmd (the replacement for DOS) has this kind of command. In any case, I don't know of any.

    We should check the autorun files of some CDs to see how they do it. If it's not compiled.
    2
    1. HostOfSeraphim Posted messages 7340 Status Contributor 1 609
       
      It wouldn't be illogical to have a command for that, but indeed, there isn't a "simple" one...

      Whereas under Linux, there is an eject command...


      --
      Configuration: Linux Debian (Lenny) / Gnome / Firefox 2
      Internet Connection: ADSL Orange 8M (Livebox)
      0
  9. thib0787 Posted messages 934 Status Member 176
     
    No, you enter the code in a text file and then you give it the .vbs extension.
    1
  10. thib0787 Posted messages 934 Status Member 176
     
    yes vbs is Visual Basic,

    and I don't know, I have Windows XP, and I enter this code in a text file that I rename from .txt to .vbs and when I click on it the CD drive opens, I don't look any further!!!
    it's XP that must handle the compilation and execution, or something like that, as long as it works ...
    1
    1. commentaire
       
      No, it's Visual Basic Script, hence the "s" at the end. ;)
      0
  11. hamza_0081 Posted messages 260 Status Member 60
     
    [[Yes, VBS is Visual Basic,

    and I don’t know, I have Windows XP, and I enter this code into a text file that I rename to .vbs instead of .txt and when I click on it, the CD drive opens, I’m not looking any further!!!
    It’s XP that must be doing the compilation and execution, or something like that, as long as it works...]]

    I believe if you installed Visual Basic, it means it is doing the compilation logically, right?
    1
  12. hamza_0081 Posted messages 260 Status Member 60
     
    Great, it works for me, but I don't know if it will work for anyone else or not?
    Because I haven't installed Visual Basic
    Where can I find tutorials for this kind of script?
    1
  13. thib0787 Posted messages 934 Status Member 176
     
    Normally, there is no need to install anything; it works on a PC that has just been reformatted (empty, without anything installed).
    1
  14. hamza_0081 Posted messages 260 Status Member 60
     
    Salut, je me lance sur les VBS et depuis j'ai découvert des tas de choses, la plus marrante c'est celle de créer un spam pour envoyer des centaines de messages sur Messenger, que ce soit Skype, MSN, Yahoo...
    C'est vraiment cool
    Merci les gars
    Bye
    1
  15. arrial Posted messages 1803 Registration date   Status Member Last intervention   251
     
    Hello,

    I'm still learning something by visiting forums, thank you.
    A cmd *.bat file can launch a basic file, and probably Pascal, Fortran, C, …?

    This can be explained by the fact that the commands used are simple (not object-oriented, …) and only involve the initial "democratizations" of machine language, for which these languages, compiled or not, were created.

    Thanks again ‼

    See you on CÇM.
    1
  16. hamza_0081 Posted messages 260 Status Member 60
     
    Hi Arrial
    this code runs a script written in VBScript with the extension *.vbs and Windows executes it using WScript; it's an interpreted language unlike Pascal, which is compiled by a compiler like Turbo
    I doubt you can run it (Pascal) via cmd, I'm not sure about Fortran? But for C and C++ you need a compiler to run scripts, whereas JavaScript and Python scripts can be run.........
    because of WSH, I believe!

    this script is not mine, I took it from another forum because I liked how they manage the commands!!
    1
  17. hamza_0081 Posted messages 260 Status Member 60
     
    hi arrial
    this code runs a script written in vbscript which has the *.vbs extension and Windows executes it using wscript, it is an interpreted language unlike Pascal which is compiled by a compiler like Turbo
    I doubt that we can run it (Pascal) through cmd, I don’t know about Fortran? but for C and C++ you need a compiler to run scripts, however, JavaScript and Python scripts can........
    because of WSH I believe!

    this script isn’t mine, I took it from another forum because I liked the way commands are managed!!
    1
  18. zemimi
     
    I had fun duplicating the code several hundred times so that the reader opens all the time, and if you want to annoy someone who's not good with computers, all you have to do is make it a scheduled task.
    1
    1. SenVara
       
      Or you can use a loop; it's so much simpler
      for example:

      iCpt=0
      Do While iCpt=0
      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
      Loop
      0
      1. LIAMSI3456 > SenVara
         
        To create loops with .vbs, you need to put 'do' at the beginning and 'loop' at the end ;)
        0
    2. tonylapatate
       
      or in VBS to open, there is:

      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

      without closing of course
      and it's even simpler and you can run it from DOS!!
      0
    3. anonyme
       
      It's very simple, type this in .vbs

      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
      X=MsgBox("Did you put the disk in?", +16, "Disk")
      For i = 0 to colCDROMs.Count - 1
      colCDROMs.Item(i).Eject
      Next ' cdrom
      End If
      0
    4. anonyme
       
      You're welcome in advance.
      0
  19. freeze
     
    the mechanical opening and closing of the reader (ejection) that makes the noise dzzzzrrrrrrrrrrrrrrrt
    0
  • 1
  • 2