Enumerate Available Drives
By Sergey - Posted on January 1st, 2008
The code below demonstrates the use of the DRIVETYPE() function.
| This is sample code. Add error handling and adjust to your requirements as necessary. |
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.
&& 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
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
Recent comments
3 weeks 2 days ago
3 weeks 2 days ago
3 weeks 2 days ago
5 weeks 3 days ago
6 weeks 5 days ago
7 weeks 19 hours ago
8 weeks 1 day ago
11 weeks 22 hours ago
12 weeks 13 hours ago
12 weeks 13 hours ago