Solved

detect cd

Posted on 2008-10-17
12
568 Views
Last Modified: 2012-06-27
batch file code that when run detect whether there is cd /dvd in cd/dvd drive of system or not and if it exists pop up message "cd exist".
0
Comment
Question by:ankur3020
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22749225
Since nobody has come up with positive answers to your questions, here is what you probably did not want to hear. Three times over: three negative answers for the price of one.

You can't do any of these things in DOS.If you're interested in a Windows Scripting Host solution, then that might be possible. The trouble with WSH is that every user has a different version, which means that things that work perfectly on your machine may give a runtime error on your user's machine; and Vista is another story entirely.

I'm guessing here, but I suspect that you want this for an install, because otherwise you would not be wanting to enumerate the host machine's drives. You simply can't write a proper install in Windows batch. It's nowhere near powerful enough. For a free and quite capable (if somewhat under-documented and exceedingly hard-to-learn) install system try Nullsoft Install at http://nsis.sf.net.
0
 

Author Comment

by:ankur3020
ID: 22750733
pls give vbs code then
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22756468
Here it is
strComputer = "."
Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk")
For Each objDisk in colDisks
 if objDisk.DriveType = 5 Then
 Wscript.Echo "DeviceID "& objDisk.DeviceID & " is a CD." 
 End if
Next

Open in new window

0
[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 

Author Comment

by:ankur3020
ID: 22758973
your code is pop up message that is specifying cd drive name not whether it has cd in drive or not. pls amend it
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22759536
I thought you would be able to manage that on your own.
strComputer = "."
Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & 
 
strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk")
For Each objDisk in colDisks
 if objDisk.DriveType = 5 Then
 Wscript.Echo "There is a CD." 
 Exit for
 End if
Next

Open in new window

0
 

Author Comment

by:ankur3020
ID: 22767521
above code is giving syntax error at line 2 pls check
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22796455
It i's giving a syntax error because 2 linebreaks got introduced while cutting and pasting. Replace lines 2-4 with the original line 2 and it works.

strComputer = "."
Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk")
For Each objDisk in colDisks
 if objDisk.DriveType = 5 Then
 Wscript.Echo "There is a CD." 
 Exit for
 End if
Next

Open in new window

0
 

Author Comment

by:ankur3020
ID: 22798281
i tried ur code, it is not working properly. there was no cd in cd rom, still it was saying there is cd" why so? pls recheck it.
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22802021
Sorry, misunderstood the question. That code is checking for the kind of drive (CD, floppy, network, etc). Not for what is in it.
0
 
LVL 5

Accepted Solution

by:
PaulKeating earned 250 total points
ID: 22802059
Try this one. It reports on the volume name of the CD. If there is no CD in the drive then the volume name will be empty.

If you don't like the message then substitute

Wscript.Echo "There is a CD." in line 6.

strComputer = "."
Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk")
For Each objDisk in colDisks
 if objDisk.DriveType = 5 and objDisk.VolumeName <> "" Then
 Wscript.Echo "DeviceID "& objDisk.DeviceID & " is a CD: " & objDisk.VolumeName
 Exit For
 End if
Next

Open in new window

0
 

Author Comment

by:ankur3020
ID: 22802572
thanks . it is working fine.
0
 

Author Closing Comment

by:ankur3020
ID: 31507379
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question