{"id":70293,"date":"2005-03-07T04:10:00","date_gmt":"2005-03-07T04:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/07\/how-can-i-tell-if-a-file-exists-on-a-cd-or-dvd-drive\/"},"modified":"2005-03-07T04:10:00","modified_gmt":"2005-03-07T04:10:00","slug":"how-can-i-tell-if-a-file-exists-on-a-cd-or-dvd-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-file-exists-on-a-cd-or-dvd-drive\/","title":{"rendered":"How Can I Tell If a File Exists on a CD or DVD Drive?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! How can I tell whether a file exists on any CD or DVD drive connected to a computer?<BR><BR>&#8212; GH<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, GH. Well, one way to do this would be to search the entire file system &#8211; including any mapped network drives &#8211; and check to see if a file (let\u2019s call it Budget.xls) can be found anywhere. Assuming you find such a file, you can then use some additional code to determine whether or not that file happens to be on a CD or DVD drive.<\/P>\n<P>That would work, but it\u2019s not very elegant and &#8211; depending on the size of your file system &#8211; could take awhile to complete. A much better method would be to tell your script to look for that file <I>only<\/I> on CD or DVD drives. And guess what: that\u2019s exactly the approach we\u2019re going to take.<\/P>\n<P>What, you expected the Scripting Guys to do something that <I>wasn\u2019t<\/I> elegant and sophisticated?<\/P>\n<P>We\u2019ll want to start by looking for the CD\/DVD drives that are connected to the computer in question, and discovering the drive letter for each of those items. Fortunately, that\u2019s easy to do; this little WMI script reports back the drive letters for all the CD\/DVD drives connected to a computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_CDROMDrive&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Drive\nNext\n<\/PRE>\n<P>After we\u2019ve identified the drive letters we can put together a WMI query that looks for a file only on those specified drives. Let\u2019s take a look at the completed script, and then we\u2019ll explain how it works:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_CDROMDrive&#8221;)<\/p>\n<p>For Each objItem in colItems\n    strDrive = objItem.Drive\n    Set colFiles = objWMIService.ExecQuery _\n        (&#8220;Select * From CIM_DataFile Where FileName = &#8216;Budget&#8217; &#8221; &amp; _\n            &#8220;AND Extension = &#8216;xls&#8217; AND Drive = &#8216;&#8221; &amp; strDrive &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>    If colFiles.Count &gt; 0 Then\n        For Each objFile in colFiles\n            Wscript.Echo objFile.Name\n        Next\n    End If\nNext\n<\/PRE>\n<P>As you can see, we start off by retrieving a collection of all the CD\/DVD drives on the computer. For each drive found (and there could be more than one), we assign the value of the <B>Drive<\/B> property (which, as we noted earlier, represents the drive letter) to a variable named strDrive. We then use a second WMI query to search that drive for a file named Budget.xls. Here\u2019s the code that does that:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n        (&#8220;Select * From CIM_DataFile Where FileName = &#8216;Budget&#8217; &#8221; &amp; _\n            &#8220;AND Extension = &#8216;xls&#8217; AND Drive = &#8216;&#8221; &amp; strDrive &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>Admittedly, the query looks a little convoluted; that\u2019s because WMI treats the file name (Budget) and the file extension (xls) as two separate properties. Because of that we have to use three parameters in our WHERE clause: the file name (<B>Budget<\/B>); the file extension (<B>xls<\/B>); and the drive we want to search. Notice that we don\u2019t hard-code in a drive letter but instead we use the variable strDrive. Like we said, at first glance it\u2019s a little complicated, but it works.<\/P>\n<P>After issuing the query, and after waiting for the script to search the CD\/DVD drive (which shouldn\u2019t take more than 15-20 seconds, tops) we check to see how many files with the name Budget.xls were found. If the collection contains at least one file (<B>colFiles.Count &gt; 0<\/B>) we echo the <B>Name<\/B> property for each file found. (If Count is equal to 0 that means we couldn\u2019t find any files named Budget.xls, so we don\u2019t do anything at all.) The script then loops around and searches the next CD\/DVD drive in the collection. This continues until each CD\/DVD drive has been searched, and any successes reported back.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell whether a file exists on any CD or DVD drive connected to a computer?&#8212; GH Hey, GH. Well, one way to do this would be to search the entire file system &#8211; including any mapped network drives &#8211; and check to see if a file (let\u2019s call it [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,34,35,3,12,5],"class_list":["post-70293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-hardware","tag-peripherals-and-devices","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell whether a file exists on any CD or DVD drive connected to a computer?&#8212; GH Hey, GH. Well, one way to do this would be to search the entire file system &#8211; including any mapped network drives &#8211; and check to see if a file (let\u2019s call it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}