WebFtpFindFirstFile Function

Action

Searches the specified directory of the given FTP session. File and directory entries are returned in the sFile string. This function lists both files and directories.

Include file

WebAPI.bdh

Syntax

WebFtpFindFirstFile( in  hFtp        : number, 
                     in  sSearchFile : string allownull, 
                     out sFile       : string, 
                     in  nMaxFile    : number, 
                     out nAttribute  : number): boolean;

Return value

  • true for the request if the directory enumeration was started successfully

  • false otherwise

Parameter Description
hFtp Valid handle to an FTP session. This handle must have been returned from a previous call to WebFtpConnect.
sSearchFile String that specifies a valid directory path or file name for the FTP server's file system. If the value of sSearchFile is NULL or if it is an empty string, it will find the first file in the current directory on the server.
sFile Parameter that receives the name of the file or directory
nMaxFile Maximum length of the string to put into sFile
nAttribute

Parameter that receives one or more of the following attribute flags:

  • WEB_FTP_FA_READONLY

  • WEB_FTP_FA_HIDDEN

  • WEB_FTP_FA_SYSTEM

  • WEB_FTP_FA_DIRECTORY

  • WEB_FTP_FA_ARCHIVE

  • WEB_FTP_FA_NORMAL

  • WEB_FTP_FA_TEMPORARY

  • WEB_FTP_FA_COMPRESSED

  • WEB_FTP_FA_NO_MORE_FILES. Signals end of directory listing

注: If the function finds no matching files, it returns false and sets the variable nAttribute to WEB_FTP_FA_NO_MORE_FILES.
注: Make sure the length of the string is greater than or equal to dwMaxFile.
注: Only one WebFtpFindFirstFile can occur at a time within a given FTP session. The enumerations, therefore, are correlated with the FTP session handle. This is because FTP protocol allows only a single directory enumeration per session. After beginning a directory enumeration with WebFtpFindFirstFile, the WebFtpFindNextFile function can be used to continue the enumeration.

Example

// Connect to the ftp server and list the root directory 
// 1) using WebFtpFindFirstFile, WebFtpFindNextFile in loops 
// 2) using only the command WebFtpListDirectory 
dcltrans 
  transaction TWebFtpListDir 
  var
    hFtp       : number;
    sFileName  : string(MAX_PATH);
    nAttribute : number;
    nFiles     : number;
    bOk        : boolean; 
  begin
    WebFtpConnect(hFtp, "standardhost", WEB_PORT_FTP, "", "");
    nFiles := 0;
 
    // Custom directory list
    bOk := WebFtpFindFirstFile(hFtp, "", sFileName,
                               MAX_PATH, nAttribute); 
    while bOk do 
      write(sFileName); write(" "); write(nAttribute); 
      writeln;
      nFiles := nFiles + 1;
      bOk := WebFtpFindNextFile(hFtp, sFileName,
                                MAX_PATH, nAttribute); 
    end;

    if nAttribute = WEB_FTP_FA_NO_MORE_FILES then 
      write(nFiles); write(" files found"); writeLn; 
    else 
      write("Error retrieving directory information!"); writeln; 
    end;
 
    // The following command does the same
    WebFtpListDirectory(hFtp, "");
    WebFtpShutdown(hFtp); 
  end TWebFtpListDir;

SilkEssential sample

Ftp.sep