Enumerate Available Drives

The code below demonstrates the use of the DRIVETYPE() function.

$SAMPLECODE$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

DIMENSION laDrives[1,2]
lnDriveCount = 0
FOR i=ASC("A") TO ASC("Z")
	lnDriveType = DRIVETYPE(CHR(i))
	DO CASE
	CASE lnDriveType = 1
		* Unknown
		lcDriveType = "UNKNOWN"
	CASE lnDriveType = 2
		* Drive with removable media
		*  	like floppy, flash drive, thumb drive, etc.
		lcDriveType = "REMOVABLE"
	CASE lnDriveType = 3
		* Drive with fixed media, like HD
		lcDriveType = "FIXED"
	CASE lnDriveType = 4
		* Remote (network) drive
		lcDriveType = "NETWORK"
	CASE lnDriveType = 5
		lcDriveType = "CDROM"
	CASE lnDriveType = 6
		lcDriveType = "RAMDISK"
	OTHERWISE
		lcDriveType = "UNKNOWN"
	ENDCASE
	IF NOT (lcDriveType = "UNKNOWN")
		lnDriveCount = lnDriveCount + 1
		DIMENSION laDrives[lnDriveCount,2]
		laDrives[lnDriveCount,1] = CHR(i)
		laDrives[lnDriveCount,2] = lcDriveType
	ENDIF
ENDFOR

The ready to use EnumerateDrives function below is based on suggestion by Bhavbhuti Nathwani. It accepts one optional parameter and returns a collection of drives. Can be used in VFP8 and later.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

*  Sample code demonstrates use of EnumerateDrives function
* Enumerate all drives
loDrives = EnumerateDrives()
* Enumerate CD-ROMs and removable drives
loDrives = EnumerateDrives("CDROM,REMOVABLE")
* Display result
FOR i=1 TO loDrives.Count
	? loDrives.GetKey(i), loDrives(i)
ENDFOR
*-----------------------------------------

FUNCTION EnumerateDrives
LPARAMETERS tcTypes2Return
* tcTypes2Return - a list of drive types to return. ALL is default
LOCAL lcTypeList, loDrives AS Collection, lnI, lnDriveType, lcDriveType, lcTypes2Return

lcTypes2Return = UPPER(EVL(tcTypes2Return, "ALL"))
loDrives = CREATEOBJECT("Collection")
lcTypeList = "UNKNOWN,REMOVABLE,FIXED,NETWORK,CDROM,RAMDISK"

FOR lnI = ASC("A") TO ASC("Z")
	lnDriveType = DRIVETYPE(CHR(lnI))
	IF BETWEEN(lnDriveType, 2, 6)
		lcDriveType = GETWORDNUM(lcTypeList, lnDriveType, [,])
		IF lcTypes2Return = "ALL" OR (lcDriveType $ lcTypes2Return)
			loDrives.Add(lcDriveType, CHR(lnI))
		ENDIF	
	ENDIF	
ENDFOR
RETURN loDrives

The EnumerateDrivesEx function below uses Shell Application object to distinguish between Floppy and Removable drives

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

 Enumerate CD-ROMs, removable drives and Floppy
loDrives = EnumerateDrivesEx("CDROM,REMOVABLE,FLOPPY")
* Display result
FOR i=1 TO loDrives.Count
	? loDrives.GetKey(i), loDrives(i)
ENDFOR
*-----------------------------------------
FUNCTION EnumerateDrivesEx
LPARAMETERS tcTypes2Return
* tcTypes2Return - a list of drive types to return. ALL is default
LOCAL lcTypeList, loDrives AS Collection, lnI, lnDriveType, lcDriveType, lcTypes2Return, loShellApp, lcTypeExtended

lcTypes2Return 	= UPPER(EVL(tcTypes2Return, "ALL"))
loDrives		= CREATEOBJECT("Collection")
lcTypeList		= "UNKNOWN,REMOVABLE,FIXED,NETWORK,CDROM,RAMDISK"
loShellApp = CREATEOBJECT("Shell.Application")

FOR lnI = ASC("A") TO ASC("Z")
	lnDriveType = DRIVETYPE(CHR(lnI))
	IF BETWEEN(lnDriveType, 2, 6)
		lcDriveType = GETWORDNUM(lcTypeList, lnDriveType, [,])
		IF lcDriveType == "REMOVABLE"  ;
				AND "FLOPPY" $ UPPER(loShellApp.NameSpace(CHR(lnI)+ ":").Self.Type)
			lcDriveType = "FLOPPY"
		ENDIF		
		IF lcTypes2Return = "ALL" OR (lcDriveType $ lcTypes2Return)
			loDrives.Add(lcDriveType, CHR(lnI))
		ENDIF	
	ENDIF	
ENDFOR
RETURN loDrives

Comments