NLST Method
Class: FTP_SERVICE
Get simple list of folders and files.
=
Object.NLST
Parameter
luData
Local file name, string or array
Type Character/Array
By reference  
Direction Input
This parameter is depends on parameter liFlags. For more informations see to example part.
This parameter cannot be empty.
If is parameter liFlags
  • _FTPS_RWF_File - The parametr is file name. The result will be save as file.
  • _FTPS_RWF_String - The parameter is string and must be send by reference. The result will be save as string.
  • _FTPS_RWF_Array - The parameter is array and must be send by reference. The result will be save as array. Each cell of array contain a one file or folder name.
Parsing to array do after calling event FTP_SERVICE::AfterNLST().
lcMask
Mask for selecting folders and files
Type Character
Direction Input
liFlags
Mode flags
Type Boolean
Direction Input
Optional  
This parameter define type of first parameter luData. List of types
ValueDescription
_FTPS_RWF_File luData is file name.
_FTPS_RWF_String luData is string.
_FTPS_RWF_Array luData is array.
lnFlag
The flags that indicate various options.
Type Numeric
Direction Input
Optional  
A passed value is equal to dwFlags parameter of API function. If this parameter ommited, then default value is FTP_TRANSFER_TYPE_UNKNOWN . Can be one of the following:
ValueDescription
FTP_TRANSFER_TYPE_ASCII Transfers the file as ASCII.
FTP_TRANSFER_TYPE_BINARY Transfers the file as binary.
Assumed from MSDN.
Return value Boolean
The return value is .T. if list is downloaded or .F. is not.
Example
LOCAL loFTP
LOCAL ARRAY laFiles(1),laFile(1)

#INCLUDE "ftp.h"
SET PROCEDURE TO ftp.prg ADDITIVE 
loFTP=CREATEOBJECT('_myftp') 

IF loFTP.OpenInternet("anonymous", "gorila@gorila.cz","192.168.2.21", "21")

    * Get "Name list" and save it as file
    =loFTP.NLST("c:\nlstx.txt","",_FTPS_RWF_File,0)
    MODI FILE "c:\nlst.txt" NOWAIT


    * Get "Name list" and return it as string
    =loFTP.NLST(@lcPom,"",_FTPS_RWF_String,0)
    ?lcPom


    * Get "Name list" and save it to array
    ?loFTP.NLST(@laFiles,"download",_FTPS_RWF_Array,0)

    * For each file get other informations
    FOR lii=1 to ALEN(lafiles)
        loFTP.GetFTPDirectoryArray(@laFile, laFiles(lii),0)
        ?
        ?laFile[1,1]
        ?laFile[1, 2]
        ?laFile[1, 3]
        ?laFile[1, 4]
        ?laFile[1, 5]
        ?laFile[1, 6]
        ?laFile[1, 7]
    NEXT

   =loFTP.CloseInternet() 
ENDIF
RELEASE PROCEDURE ftp.prg

DEFINE CLASS _myFTP AS FTP_SERVICE
   PROCEDURE BeforeNLST(luData,lcMask,liFlags,lnFlag)
      ?PROGRAM(16)
      ?CHR(9),luData,lcMask,liFlags,lnFlag
   ENDPROC

   PROCEDURE AfterNLST(luData,lcMask,liFlags,lnFlag,liResult)
      ?PROGRAM(16)
      ?CHR(9),luData,lcMask,liFlags,lnFlag,liResult
   ENDPROC

   PROCEDURE AtNLST(luData,lcMask,liFlags,lnFlag,lcBuffer,liResult)
      ?PROGRAM(16)
      ?CHR(9),luData,lcMask,liFlags,lnFlag,lcBuffer,liResult
   ENDPROC
ENDDEFINE 
See also
Expand/Collapse source code of procedure NLST Source Code
      LPARAMETERS OUTREF luData, INP lcMask,OPT_INP liFlags,OPT_INP lnFlag
      EXTERNAL ARRAY luData
      LOCAL liResult, lihFTP,lii,lcBuffer,lcData,liData,liEnd,liCount,llRet

      liFlags=IIF(PCOUNT()<3,_FTPS_RWF_File,liFlags)
      lnFlag=IIF(PCOUNT()<4,FTP_TRANSFER_TYPE_ASCII,lnFlag)

      IF This.OpenFTPConnection(This.cCurrentDir)     && Open an FTP Handle
         IF BITTEST(liFlags,2) && Array?
            DIMENSION luData [1, 1]
            luData [1, 1] = .NULL.
         ENDIF
         lihFTP=0

         This.BeforeNLST(@luData,@lcMask,liFlags,lnFlag)

         liResult=IIF(This._FTPCommand("NLST "+lcMask,FTP_TRANSFER_TYPE_ASCII,0,@lihFTP),1,0)
         IF liResult #0 && OK, FTP list is openned
            llRet=.T.         
            lii=0
            liData=500
            STORE 1 TO fResult,liRead
            lcData=""

            DO WHILE liRead>0
               liRead=0
               lcBuffer=SPACE(liData)
               liResult = InternetReadFile(lihFTP, @lcBuffer, liData, @liRead)
               =This.GetExtendedError()
               This.AtNLST(@luData,@lcMask,liFlags,lnFlag,@lcBuffer,liResult)

               lcData=lcData+LEFT(lcBuffer,liRead)
               lii=lii+liRead
            ENDDO
            =InternetCloseHandle(lihFTP)  

            DO CASE
               CASE liResult=0

               CASE !BITTEST(liFlags,0) AND !BITTEST(liFlags,2) && File?
                    IF STRTOFILE(lcData,luData)=0
                       This.SetExtendedError(_FTPS_UE_CWF,"")
                       llRet=.F.
                    ENDIF

               CASE BITTEST(liFlags,0) && String?
                    luData=lcData

               CASE BITTEST(liFlags,2) && Array?
                    * parse data by CRLF
                    IF AT("\015\012",lcData)>0 && EPLF
                       lcData=STRTRAN(lcData,"\015\012",CRLF)
                    ENDIF
                    IF VAL(STRTRAN(SUBS(VERSION(),LEN("Visual FoxPro ")+1,2),"0",""))>=6
                       liCount=ALINES(luData,lcData)
                    ELSE
                       liCount=OCCURS(CRLF,lcData)
                       IF liCount>0 THEN
                          DIME luData[liCount, 1]
                          lii=1  
                          liEnd=AT(CRLF,lcData,1)
                          DO WHILE liEnd>0
                             luData[lii, 1]=LEFT(lcData,liEnd-1)
                             lcData=SUBSTR(lcData,liEnd+2)
                             liEnd=AT(CRLF,lcData,1)
                             lii=lii+1  
                          ENDDO
                       ENDIF
                    ENDIF
            ENDCASE
         ENDIF
         This.AfterNLST(luData,lcMask,liFlags,lnFlag,liResult)
         =IIF(This.lMultiOperations,.T.,This.CloseFTPConnection())   && Close FTP Handle
         RETURN llRet
      ELSE
         =IIF(This.lMultiOperations,.T.,This.CloseFTPConnection())   && Close FTP Handle
         RETURN .F.     && Unable to get FTP Connection   
      ENDIF