KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q139548: How to Use the GetDiskFreeSpace API Call

Article: Q139548
Product(s): Microsoft FoxPro
Version(s): 3.00
Operating System(s): 
Keyword(s): kbcode
Last Modified: 30-JUL-1999

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual FoxPro for Windows, version 3.0 
-------------------------------------------------------------------------------

SUMMARY
=======

By using the GetDiskFreeSpace API call, you can determine how much disk space a
drive has, how much is used, and how much is free. This article explains how to
use and call the Win32 GetDiskFreeSpace API.

MORE INFORMATION
================

In many cases, such as backing up or restoring user data, you need to know
various characteristics about the media with which you are working (such as
whether or not it is fixed or removable) and about the amount of available space
on that media.

By using the GetDiskFreeSpace API call, you can derive the low-level information
about the disk (such as bytes per sector and sectors per cluster), and then use
that information for your calculations.

The following code demonstrates the syntax and use of this API call and gives
some of the more common calculations you may need to perform.

Code Sample
-----------

  **---------------------------------------------------------------**
  ** Program: Getspace.prg                                         **
  ** Purpose: Demonstrates how to declare and call the Win32       **
  **          GetDiskFreeSpace API.                               **
  **---------------------------------------------------------------**
  PUBLIC  lpRootPathName, ;
          lpSectsPerCluster, ;
          lpBytesPerSector, ;
          lpNumberOfFreeClusters, ;
          lpTotalNumberOfClusters

  lpRootPathName       = "A:\"
  lpSectsPerCluster    = 0
  lpBytesPerSect       = 0
  lpNumOfFreeClusters  = 0
  lpTotNumOfClusters   = 0

  DECLARE INTEGER GetDiskFreeSpace IN Win32API AS GetDiskSpace ;
          STRING  @lpRootPathName, ;
          INTEGER @lpSectsPerCluster, ;
          INTEGER @lpBytesPerSect, ;
          INTEGER @lpNumOfFreeClusters, ;
          INTEGER @lpTotNumOfClusters

  RetVal=GetDiskSpace(@lpRootPathName, @lpSectsPerCluster, @lpBytesPerSect, ;
          @lpNumOfFreeClusters, @lpTotNumOfClusters)

  DEFINE WINDOW ShowInfo FROM 0,0 TO 12,70 ;
          FLOAT CLOSE ;
          TITLES "Drive Information for " + ;
          ALLTRIM(lpRootPathName) ;
          FONT "Courier",10

  ACTIVATE WINDOW ShowInfo
  MOVE WINDOW ShowInfo CENTER

  @ 0,1 SAY "Drive & path name            : " + ;
     ALLTRIM(lpRootPathName)
  @ 1,1 SAY "Sectors per cluster          : " + ;
     ALLTRIM(STR(lpSectsPerCluster))
  @ 2,1 SAY "Bytes per Sector             : " + ;
     ALLTRIM(STR(lpBytesPerSect))
  @ 3,1 SAY "Number of free clusters      : " + ;
     ALLTRIM(STR(lpNumOfFreeClusters))
  @ 4,1 SAY "Total number of clusters     : " + ;
     ALLTRIM(STR(lpTotNumOfClusters))
  @ 5,1 SAY "==================================================="
  @ 6,1 SAY "Total disk space             : " + ;
     ALLTRIM(STR(lpSectsPerCluster * lpBytesPerSect * lpTotNumOfClusters))
  @ 7,1 SAY "Free disk space              : " + ;
     ALLTRIM(STR(lpSectsPerCluster * lpBytesPerSect * lpNumOfFreeClusters))
  @ 8,1 SAY "Used disk space              : " + ;
     ALLTRIM(STR(lpSectsPerCluster * lpBytesPerSect * ;
     (lpTotNumOfClusters - lpNumOfFreeClusters)))

  **--------------------------<End Code>---------------------------**

When this code is run on a 1.44-megabyte floppy disk, the following output is
what you would get if the floppy disk is a new, formatted floppy disk that
contains no data:

Drive & path name        : A:\ 
Sectors per cluster      : 1
Bytes per sector         : 512
Number of free clusters  : 2847
Total number of clusters : 2847
===================================
Total disk space         : 1457664
Free disk space          : 1457664
Used disk space          : 0

Using this API call, you can quickly and easily determine how much disk space you
have available for your program. A related disk API (GetVolumeInformation)
provides information about the disk physical characteristics such as the file
system and file compression.

For additional information on this API function, please see the
"GetDiskFreeSpace" topic in the Win32API help file. Note that this help file is
available only on the compact disc version of Visual FoxPro; it is not shipped
with the disk version. For more information, please see the following article in
the Microsoft Knowledge Base:

  Q139172 PRB: Visual FoxPro CD-ROM Compact Disc Includes Extra Files

For information about the DECLARE - DLL command, please search the Visual FoxPro
Help file.

Additional query words: VFoxWin drive volume space free attributes

======================================================================
Keywords          : kbcode 
Technology        : kbVFPsearch kbAudDeveloper kbVFP300
Version           : 3.00

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.