How to Determine the Number of Files Currently Open

Last reviewed: November 21, 1995
Article ID: Q119352
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5x, 2.6
  • Microsoft FoxPro for MS-DOS, versions 2.5x, 2.6

SUMMARY

Although the SYS(2010) function can be used in FoxPro for MS-DOS to return the maximum number of files specified in the CONFIG.SYS file (in FoxPro for Windows, this function always returns 255), there is no function that will return the number of files currently in use within the current FoxPro session. The user-defined function (UDF) shown below will return the number of files open within the current FoxPro session at the time the function is called.

MORE INFORMATION

Create the function OPEND() as either a separate .PRG file or as a FUNCTION subroutine in the program that calls it. In this example, it is used as a FUNCTION subroutine within a file named FYLCOUNT.PRG:

   * FYLCOUNT.PRG program file, containing the UDF Opend
   *

   x = OPEND()
   WAIT WINDOW "There are currently "+ALLTRIM(STR(x))+" files opened."

   FUNCTION opend

      SET TEXTMERGE TO SYS(2015)   && open a temporary text file
      cnt=_TEXT-1                  && subtract 1 from its handle
      SET TEXTMERGE TO             && close the temporary file

   RETURN cnt                      && send back the number of files

When the program is run, the file handle for the temporary text merge file is allocated (an error will be generated if not enough handles are available).

This handle is stored in the system variable _TEXT. Because the current count is desired, 1 is subtracted from the number in _TEXT to eliminate the temporary text file from being included in the count of open file handles. This result is stored in the variable "cnt". The temporary file is closed, returning the file handle it used to the remaining file handles available. As nothing was written to the file, the filename was not written to the disk directory, and thus requires no maintenance to remove it from disk.


Additional reference words: FoxDos FoxWin 2.50 2.50a 2.50b 2.60 fopen
ferror
handles
KBCategory: kbprg kbcode
KBSubcategory: FxenvAutoexec


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.

Last reviewed: November 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.