CFtpFileFind Class

Aids in Internet file searches of FTP servers.

Syntax

class CFtpFileFind : public CFileFind

Members

Public Constructors

Name Description
CFtpFileFind::CFtpFileFind Constructs a CFtpFileFind object.

Public Methods

Name Description
CFtpFileFind::FindFile Finds a file on a FTP server.
CFtpFileFind::FindNextFile Continues a file search from a previous call to FindFile.
CFtpFileFind::GetFileURL Gets the URL, including path, of the found file.

Remarks

CFtpFileFind includes member functions that begin a search, locate a file, and return the URL or other descriptive information about the file.

Other MFC classes designed for Internet and local file searched include CGopherFileFind and CFileFind. Together with CFtpFileFind, these classes provide a seamless mechanism for the client to find specific files, regardless of the server protocol or file type (either a local machine or a remote server). Note that there is no MFC class for searching on HTTP servers because HTTP does not support the direct file manipulation required for searches.

For more information about how to use CFtpFileFind and the other WinInet classes, see the article Internet Programming with WinInet.

Example

The following code demonstrates how to enumerate all files in the current directory of the FTP server.

// create a session object to initialize WININET library
// Default parameters mean the access method in the registry
// (that is, set by the "Internet" icon in the Control Panel)
// will be used.

CInternetSession sess(_T("My FTP Session"));

CFtpConnection *pConnect = NULL;

try
{
   // Request a connection to ftp.microsoft.com. Default
   // parameters mean that we'll try with username = ANONYMOUS
   // and password set to the machine name @ domain name
   pConnect = sess.GetFtpConnection(_T("ftp.microsoft.com"));

   // use a file find object to enumerate files
   CFtpFileFind finder(pConnect);

   // start looping
   BOOL bWorking = finder.FindFile(_T("*"));

   while (bWorking)
   {
      bWorking = finder.FindNextFile();
      _tprintf_s(_T("%s\n"), (LPCTSTR)finder.GetFileURL());
   }
}
catch (CInternetException *pEx)
{
   TCHAR sz[1024];
   pEx->GetErrorMessage(sz, 1024);
   _tprintf_s(_T("ERROR!  %s\n"), sz);
   pEx->Delete();
}

// if the connection is open, close it
if (pConnect != NULL)
{
   pConnect->Close();
   delete pConnect;
}

Inheritance Hierarchy

CObject

CFileFind

CFtpFileFind

Requirements

Header: afxinet.h

CFtpFileFind::CFtpFileFind

This member function is called to construct a CFtpFileFind object.

explicit CFtpFileFind(
    CFtpConnection* pConnection,
    DWORD_PTR dwContext = 1);

Parameters

pConnection
A pointer to a CFtpConnection object. You can obtain an FTP connection by calling CInternetSession::GetFtpConnection.

dwContext
The context identifier for the CFtpFileFind object. See Remarks for more information about this parameter.

Remarks

The default value for dwContext is sent by MFC to the CFtpFileFind object from the CInternetSession object that created the CFtpFileFind object. You can override the default to set the context identifier to a value of your choosing. The context identifier is returned to CInternetSession::OnStatusCallback to provide status on the object with which it is identified. See the article Internet First Steps: WinInet for more information about the context identifier.

Example

See the example in the class overview earlier in this topic.

CFtpFileFind::FindFile

Call this member function to find an FTP file.

virtual BOOL FindFile(
    LPCTSTR pstrName = NULL,
    DWORD dwFlags = INTERNET_FLAG_RELOAD);

Parameters

pstrName
A pointer to a string containing the name of the file to find. If NULL, the call will perform a wildcard search (*).

dwFlags
The flags describing how to handle this session. These flags can be combined with the bitwise OR operator (|) and are as follows:

  • INTERNET_FLAG_RELOAD Get the data from the wire even if it is locally cached. This is the default flag.

  • INTERNET_FLAG_DONT_CACHE Do not cache the data, either locally or in any gateways.

  • INTERNET_FLAG_RAW_DATA Override the default to return the raw data ( WIN32_FIND_DATA structures for FTP).

  • INTERNET_FLAG_SECURE Secures transactions on the wire with Secure Sockets Layer or PCT. This flag is applicable to HTTP requests only.

  • INTERNET_FLAG_EXISTING_CONNECT If possible, reuse the existing connections to the server for new FindFile requests instead of creating a new session for each request.

Return Value

Nonzero if successful; otherwise 0. To get extended error information, call the Win32 function GetLastError.

Remarks

After calling FindFile to retrieve the first FTP file, you can call FindNextFile to retrieve subsequent FTP files.

Example

See the earlier example in this topic.

CFtpFileFind::FindNextFile

Call this member function to continue a file search begun with a call to the FindFile member function.

virtual BOOL FindNextFile();

Return Value

Nonzero if there are more files; zero if the file found is the last one in the directory or if an error occurred. To get extended error information, call the Win32 function GetLastError. If the file found is the last file in the directory, or if no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

Remarks

You must call this function at least once before calling any attribute function (see CFileFind::FindNextFile).

FindNextFile wraps the Win32 function FindNextFile.

Example

See the example earlier in this topic.

CFtpFileFind::GetFileURL

Call this member function to get the URL of the specified file.

CString GetFileURL() const;

Return Value

The file and path of the Universal Resource Locator (URL).

Remarks

GetFileURL is similar to the member function CFileFind::GetFilePath, except that it returns the URL in the form ftp://moose/dir/file.txt.

See also

CFileFind Class
Hierarchy Chart
CGopherFileFind Class
CInternetFile Class
CGopherFile Class
CHttpFile Class