How to Return Only the Filename from GETFILE() Function

ID: Q103683

The information in this article applies to:

SUMMARY

The GETFILE() function returns the whole path and filename of a selected file. To return only the filename, you can use the procedure shown below to remove the path information.

MORE INFORMATION

To return only the filename for the GETFILE() function, you can establish the sample FILENAME procedure shown below in a procedure file. The following code demonstrates how to call the FILENAME() function in a program:

   fvar = filename(GETFILE())
   ? fvar

SAMPLE CODE FOR THE FILENAME PROCEDURE

FoxPro for MS-DOS and FoxPro for Windows

   PROCEDURE filename
   PARAMETER mfilename
   SET TALK OFF
   IF .NOT. EMPTY(mfilename)
      position = RAT("\",mfilename)
      mfname = SUBSTR(mfilename,position+1)
      mpath = SUBSTR(mfilename,1,position)
      RETURN mfname  && Modify to 'RETURN mpath' to return path only
   ELSE
      RETURN "No File Was Selected"
   ENDIF

FoxBASE+/Mac

   PROCEDURE filename
   PARAMETER mfilename
   SET TALK OFF
   SET EXACT ON
      IF mfilename <> ""
         position = LEN(mfilename)
         char = " "
         DO WHILE char <> ":"
            char = SUBSTR(mfilename,position,1)
            position = position - 1
         ENDDO
         mfname = SUBSTR(mfilename,position+2,(LEN(mfilename) ;
            - (position +2)))
         mpath = SUBSTR(mfilename,2,position) && Modify to 'RETURN ;
            mpath' to return path only
         RETURN mfname
      ELSE
         RETURN "No file was selected"
      ENDIF
   SET EXACT OFF

Additional reference words: FoxDos FoxWin 1.02 2.00 2.50 2.50a KBCategory: kbprg KBSubcategory: FxprgGeneral

Last Reviewed: June 27, 1995