'Globals '**************************************************************** 'Windows API/Global Declarations for :Determining Free Disk Space In Win95 '**************************************************************** Public Type RANDYS_OWN_DRIVE_INFO DrvSectors As Long DrvBytesPerSectorAs Long DrvFreeClusters As Long DrvTotalClustersAs Long DrvSpaceFreeAs Long DrvSpaceUsedAs Long DrvSpaceTotalAs Long End Type Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" _ (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" _ (ByVal lpRootPathName As String, _ lpSectorsPerCluster As Long, _ lpBytesPerSector As Long, _ lpNumberOfFreeClusters As Long, _ lpTotalNumberOfClusters As Long) As Long 'Code '**************************************************************** ' Name: Determining Free Disk Space In Win95 ' Description:The code to determine clusters, sectors, used ' and free space on a release version of Win95. ' By: VB Net (Randy Birch) ' ' Inputs:None ' Returns:None ' Assumes:Start a new project, and to the form add 2 command buttons (cmdVolumeInfo and cmdEnd), and a combo box (Combo1) ' Side Effects:Note: the routine presented here will return incorrect results on volumes over 2 gigabytes, as supported by the FAT32 partitions implemented in Windows95 OEM Service Pack 2. Microsoft recently introduced the Windows95 OEM Service Pack 2. This version is provided only to OEMs who provide the Windows95 platform pre-installed on manufactured machines, and is not generally available as a release version to the public. Although not in general release, the existence of this service pack raises considerations for application developers where the application is destined for an unknown build of Win95. The OEM Service Pack 2 version of Windows95 provides, among other enhancements, optional support for FAT32 partitions, allowing a single partition of up to 5 gigabytes with a cluster size of only 4k. The current "street-release" version of Windows95 supports only partitions or drives up to 2 gigabytes in size. Using the GetDiskFreeSpace code above on drives partitioned and formatted using the newer Service Pack 2 and the FAT32 option will return incorrect values. On these partitions/drives, the correct API to use is GetDiskFreeSpaceEx. As an example, the illustration above shows my Drive C total space at 2.147 gig, the free space at 1.948 gig, and the used space at 198 meg. I am actually running the OEM Service Pack 2 version of Win95 with the FAT32 partitions (allowing single partitions on both of my 2.5 and 3.2 gig drives); the actual statistics for my C drive is 2.54 gig total space, with free space of 1.942 gig and used space of 538 meg. On a traditional FAT16 drive, the above code returns the correct statistics. To assure that the correct method is used to calculate the free space, Microsoft recommends obtaining the user's version of Windows using GetVersionEx prior to calling the free space APIs. I hope to have this FAT32 method available soon. ' 'Code provided by Planet Source Code(tm) 'as is', without ' warranties as to performance, fitness, merchantability, ' and any other warranty (whether expressed or implied). '**************************************************************** Add the following code to Form1. Private Sub Form_Load() Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 LoadAvailableDrives Combo1.ListIndex = 1 End Sub Private Sub cmdEnd_Click() Unload Me End End Sub Private Sub cmdVolumeInfo_Click() ' 'Calls other functions to provide the info. ' 'Data is stored in my own user-defined type. Dim RDI As RANDYS_OWN_DRIVE_INFO Dim r As Long Dim RootPathName As String ' 'get the drive to find RootPathName$ = Combo1.List(Combo1.ListIndex) ' 'get the drive's disk parameters r& = rgbGetDiskFreeSpaceRDI(RootPathName$, RDI) ' 'show the results Cls Print Print " Drive Statistics for ", ": "; UCase$(RootPathName$) Print Print " No of Sectors", ": "; RDI.DrvSectors Print " Bytes per sector", ": "; RDI.DrvBytesPerSector Print " Clusters Free", ": "; RDI.DrvFreeClusters Print " Total Clusters", ": "; RDI.DrvTotalClusters Print " Drive Space Used", ": "; Format$(RDI.DrvSpaceUsed, "###,###,###,##0") & " bytes" Print " Drive Space Free", ": "; Format$(RDI.DrvSpaceFree, "###,###,###,##0") & " bytes" Print " Total Drive Size", ": "; Format$(RDI.DrvSpaceTotal, "###,###,###,##0") & " bytes" End Sub Private Sub LoadAvailableDrives() Dim r A Long Dim DriveSize As Long Dim lpBuffer As String Dim currDrive As String ' 'get the list of all available drives lpBuffer$ = rgbGetLogicalDriveStrings() ' 'Separate the drive strings and add them to the combobox. Do Until lpBuffer$ = Chr$(0) ' 'strip off one drive item from the lpBuffer$ currDrive$ = StripNulls$(lpBuffer$) ' 'add the drive to the combo list Combo1.AddItem currDrive$ Loop End Sub Private Function rgbGetDiskFreeSpaceRDI(RootPathName$, RDI As RANDYS_OWN_DRIVE_INFO) As Long ' 'returns data about the selected drive. ' 'Passed is the RootPathName$; the other ' 'variables are filled in here. Dim r As Long r& = GetDiskFreeSpace(RootPathName$, RDI.DrvSectors, RDI.DrvBytesPerSector, RDI.DrvFreeClusters, RDI.DrvTotalClusters) RDI.DrvSpaceTotal = (RDI.DrvSectors * RDI.DrvBytesPerSector * RDI.DrvTotalClusters) RDI.DrvSpaceFree = (RDI.DrvSectors * RDI.DrvBytesPerSector * RDI.DrvFreeClusters) RDI.DrvSpaceUsed = RDI.DrvSpaceTotal - RDI.DrvSpaceFree rgbGetDiskFreeSpaceRDI& = r& End Function Private Function rgbGetLogicalDriveStrings() As String ' 'returns a single string of available drive ' 'letters, each separated by a space ' '(i.e. a:\ c:\ d:\), suitable for display Dim r As Long Dim i As Integer Dim lpBuffer As String lpBuffer$ = Space$(64) r& = GetLogicalDriveStrings(Len(lpBuffer$), lpBuffer$) lpBuffer$ = Trim$(lpBuffer$) rgbGetLogicalDriveStrings = lpBuffer$ End Function Private Function StripNulls(startStrg$) As String 'Take a string separated by a Chr$(0), and split off 1 item, ' and 'shorten the string so that the next item is ready for remov ' al. Dim c As Integer Dim item As String c% = 1 Do If Mid$(startStrg$, c%, 1) = Chr$(0) Then item$ = Mid$(startStrg$, 1, c% - 1) startStrg$ = Mid$(startStrg$, c% + 1, Len(startStrg$)) StripNulls$ = item$ Exit Function End If c% = c% + 1 Loop End Function