Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get CD drive letter

66 views
Skip to first unread message

Docfxit

unread,
Jun 10, 2017, 1:46:10 AM6/10/17
to
I'm trying to get the CD drive letter with this code:

SetLocal
Set "CDDrive="
For /F "Tokens=*" %%a In ('WMIC LogicalDisk WHERE^
"DriveType=5 AND Access=1 OR Access=3" Get Caption 2^>Nul') Do For %%b In (
%%a) Do If Exist %%b\I386\ Set "CDDrive=%%b"
If Not Defined CDDrive Exit/B

This is the output I am getting:
E:\>SetLocal

E:\>Set "CDDrive="

E:\>For /F "Tokens=*" %a In ('WMIC LogicalDisk WHERE "DriveType=5 AND Access=1 OR Access=3" Get Caption 2>Nul') Do For %b In (%a) Do If Exist %b\I386\ Set "CDDrive=%b"

E:\>For %b In (Caption
) Do If Exist %b\I386\ Set "CDDrive=%b"

E:\>If Exist Caption\I386\ Set "CDDrive=Caption"

E:\>For %b In (D:
) Do If Exist %b\I386\ Set "CDDrive=%b"

E:\>If Exist D:\I386\ Set "CDDrive=D:"

E:\>For %b In (
) Do If Exist %b\I386\ Set "CDDrive=%b"

E:\>If Not Defined CDDrive Exit/B

Does anyone know why CDDrive is not defined?

Thanks,

Docfxit

petu...@googlemail.com

unread,
Jun 10, 2017, 8:38:10 AM6/10/17
to
The code you have provided appears to have been written to determine the optical drive letter associated with a Windows setup installation disk. Do you have a Windows CD/DVD in your optical drive?

petu...@googlemail.com

unread,
Jun 10, 2017, 10:23:08 AM6/10/17
to
Also, if you have got a Windows setup disk loaded depending upon its OS you may not have a directory in it's root named I386. If that is the case you may need to change it to another directory such as boot, support, tools or upgrade or even to a known file such as setup.exe or autorun.inf.

::----- Start getCDD.cmd -----

@Echo Off
Set "CDDrive="
For /F "Tokens=*" %%a In ('WMIC LogicalDisk WHERE^
"DriveType=5 AND Access=1 OR Access=3" Get Caption 2^>Nul') Do For %%b In (%%a
) Do If Exist %%b\autorun.inf Set "CDDrive=%%b"
If Not Defined CDDrive Exit/B
Echo( [%CDDrive%]
Pause

::------ End getCDD.cmd ------

Each line has been prepended with two spaces, any line which doesn't start with two spaces or more has wrapped. (You may remove those two spaces.)

Tom Del Rosso

unread,
Jun 10, 2017, 10:44:58 AM6/10/17
to
Docfxit wrote:
> I'm trying to get the CD drive letter [SNIP]


for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
fsutil fsinfo drivetype %%d:|find "CD-ROM Drive"
if not errorlevel 1 set CDdrive=%%d
)
echo %CDdrive%


Tom Del Rosso

unread,
Jun 10, 2017, 10:48:09 AM6/10/17
to
Docfxit wrote:
> I'm trying to get the CD drive letter [SNIP]

I left out the first line...


set "CDdrive="

Tom Del Rosso

unread,
Jun 10, 2017, 10:54:42 AM6/10/17
to
Docfxit wrote:
> I'm trying to get the CD drive letter [SNIP]

It would be a little faster if you jump out of the loop, and it will now
find the first CD drive instead of the last one...


set "CDdrive="
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
fsutil fsinfo drivetype %%d:|find "CD-ROM Drive"
if not errorlevel 1 set CDdrive=%%d
if defined CDdrive goto :CDfound
)
:CDfound
if defined CDdrive (echo CD drive=%CDdrive%:) else (echo No CD drive
found.)



Docfxit

unread,
Jun 10, 2017, 2:36:08 PM6/10/17
to
This is really great. You are 100% correct. This code was taken from a bat file that used an XP install disk which had a folder i386. I am now using an install disk for Win7 which doesn't have i386. They both have autorun.inf. I wasn't looking at that. I was looking at the output that showed it found the D: drive but it wasn't defined.

Thank you very much for fixing that for me.
You guys are really great.

Thanks,
Docfxit

Tom Del Rosso

unread,
Jun 10, 2017, 10:00:37 PM6/10/17
to
Docfxit wrote:
> They both have autorun.inf.
[snip]

FWIW my method doesn't depend on autorun either. It also identifies the
right letter if there is no disc or if the drawer is open.

To detect if there is no disc you could take the CDdrive variable and do
this:

dir CDdrive:\ >nul 2>nul
if errorlevel 1 echo No disc.



Kerr Mudd-John

unread,
Jun 11, 2017, 6:21:33 AM6/11/17
to
On Sat, 10 Jun 2017 15:54:41 +0100, Tom Del Rosso <fizzbin...@that-google-mail-domain.com> wrote:

> Docfxit wrote:
>> I'm trying to get the CD drive letter [SNIP]
>
> It would be a little faster if you jump out of the loop, and it will now
> find the first CD drive instead of the last one...
>
As I move my CD drive letter to Z: (and it's generally at the end); it might be better to reverse the alphabet: - if you're feeling wild you might even drop the C (it's unlikely all you have is a CD drive, in which case you needn't worry about which drive to use!)

>
> set "CDdrive="
> for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (

for %%d in (z y x v u t s r q p o n m l k j i h g f e d c) do (

> fsutil fsinfo drivetype %%d:|find "CD-ROM Drive"
> if not errorlevel 1 set CDdrive=%%d
> if defined CDdrive goto :CDfound
> )
> :CDfound
> if defined CDdrive (echo CD drive=%CDdrive%:) else (echo No CD drive
> found.)
>
>
>
>


--
Bah, and indeed, Humbug

petu...@googlemail.com

unread,
Jun 11, 2017, 7:33:53 AM6/11/17
to
Depending upon your situation and version of OS you may need to run this elevated to use FSUTIL.

petu...@googlemail.com

unread,
Jun 11, 2017, 8:13:04 AM6/11/17
to
It should be noted that there is generally little need to detect the drive letter for an empty optical drive.

The OP is using a method to detect if an optical drive exists, DRIVETYPE=5, and if there is a readable disk loaded, ACCESS=1|3. The only thing to check at this pont is the the specific disk required is the one inserted. Only the OP can decide on this, however I would probably go with something more unique than an autorun.inf file which whilst it exists on both their setup disks exists on many others too. Both XP and 7 I believe have \Support\Tools so would be my preference to replace I386.

::----- Start getWinCD.cmd -----

@Echo Off
Set "WinCDD="
For /F %%A In ('MountVol^|FindStr/I [D-Z]:\\'
) Do If Exist %%~dA\Support\Tools\ Set "WinCDD=%%A"
If Not Defined WinCDD Exit/B
Echo( [%WinCDD%]
Pause

::------ End getWinCD.cmd ------

Tom Del Rosso

unread,
Jun 11, 2017, 3:12:07 PM6/11/17
to
Kerr Mudd-John wrote:
> On Sat, 10 Jun 2017 15:54:41 +0100, Tom Del Rosso
> <fizzbin...@that-google-mail-domain.com> wrote:
>> Docfxit wrote:
>>> I'm trying to get the CD drive letter [SNIP]
>>
>> It would be a little faster if you jump out of the loop, and it will
>> now find the first CD drive instead of the last one...
>>
> As I move my CD drive letter to Z: (and it's generally at the end);
> it might be better to reverse the alphabet: - if you're feeling wild
> you might even drop the C (it's unlikely all you have is a CD drive,
> in which case you needn't worry about which drive to use!)

The boot drive doesn't have to be C: and the CD can be C:.

It can happen if you format during the Windows install and then reboot
before completing. I did it once by accident because the process was
interrupted.




Tom Del Rosso

unread,
Jun 11, 2017, 3:14:46 PM6/11/17
to
petu...@googlemail.com wrote:
>
> It should be noted that there is generally little need to detect the
> drive letter for an empty optical drive.

A script could be started before the disc is inserted. It's just part
of making a program robust.



Tom Del Rosso

unread,
Jun 11, 2017, 3:23:12 PM6/11/17
to
That's true, but it can be dealt with if needed.



Kerr Mudd-John

unread,
Jun 12, 2017, 4:31:25 AM6/12/17
to
Yes, it's possible; no it's not at all likely.

Tom Del Rosso

unread,
Jun 12, 2017, 11:50:35 AM6/12/17
to
I don't care if it's likely. What am I supposed to do - save 2 bytes by
leaving the letter C out of the script line? I write programs for
reliability and portability. I left out A and B because they are
impossible (although you could duplicate the letter with A or B with
subst, there would still be the original letter).



Kerr Mudd-John

unread,
Jun 13, 2017, 8:17:58 AM6/13/17
to
You seem very focussed on that minor point; the major point was reversal of the letter order; up to you. I've said my piece.

Ammammata

unread,
Jun 13, 2017, 10:30:57 AM6/13/17
to
Il giorno Sat 10 Jun 2017 07:46:09a, *Docfxit* ha inviato su
alt.msdos.batch.nt il messaggio news:4ea55f0d-a300-42b7-a16d-
45b310...@googlegroups.com. Vediamo cosa ha scritto:

>
> I'm trying to get the CD drive letter with this code:
>
>

you can use the batch that's on the boot floppy disk of windows 98

--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
>>>>> http://www.bb2002.it :) <<<<<
........... [ al lavoro ] ...........

Kerr Mudd-John

unread,
Jun 14, 2017, 5:31:34 AM6/14/17
to
On Tue, 13 Jun 2017 15:30:54 +0100, Ammammata <amma...@tiscalinet.it> wrote:

> Il giorno Sat 10 Jun 2017 07:46:09a, *Docfxit* ha inviato su
> alt.msdos.batch.nt il messaggio news:4ea55f0d-a300-42b7-a16d-
> 45b310...@googlegroups.com. Vediamo cosa ha scritto:
>
>>
>> I'm trying to get the CD drive letter with this code:
>>
>>
>
> you can use the batch that's on the boot floppy disk of windows 98
>

If you're doing that (loading a DOS based 3rdparty CD driver) then you can use erm the Freedos one that allows you to specify the drive letter.

found it: Shsucdx

Kenny McCormack

unread,
Jul 8, 2017, 6:44:11 AM7/8/17
to
In article <ohmd1h$6v6$1...@dont-email.me>,
...
>I don't care if it's likely. What am I supposed to do - save 2 bytes by
>leaving the letter C out of the script line? I write programs for
>reliability and portability. I left out A and B because they are
>impossible (although you could duplicate the letter with A or B with
>subst, there would still be the original letter).

Actually, I often have CD drives at A and/or B.

(I use a "virtual CD drive" utility - and specifically make it show up as A
and/or B since these letters are otherwise unused)

--
It's possible that leasing office space to a Starbucks is a greater liability
in today's GOP than is hitting your mother on the head with a hammer.
0 new messages