Share via

CFtpFileFind Class

Aids in Internet file searches of FTP servers.


class CFtpFileFind : public CFileFind


Public Constructors

Name Description
CFtpFileFind::CFtpFileFind Constructs a CFtpFileFind object.

Public Methods

Name Description
CFtpFileFind::FindFile Finds a file on an 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.


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). There's no MFC class for searching on HTTP servers because HTTP doesn't 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.


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;

   // 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);

// if the connection is open, close it
if (pConnect != NULL)
   delete pConnect;

Inheritance Hierarchy





Header: afxinet.h


This member function is called to construct a CFtpFileFind object.

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


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

The context identifier for the CFtpFileFind object. For more information, see the following 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's identified. See the article Internet First Steps: WinInet for more information about the context identifier.


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


Call this member function to find an FTP file.

virtual BOOL FindFile(
    LPCTSTR pstrName = NULL,


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

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's locally cached. This is the default flag.

  • INTERNET_FLAG_DONT_CACHE Don't 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 applies 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.


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


See the earlier example in this topic.


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.


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

FindNextFile wraps the Win32 function FindNextFile.


See the example earlier in this topic.


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).


GetFileURL is similar to the member function CFileFind::GetFilePath except that it provides the result in URL format. As with CFileFind::GetFilePath, the result doesn't include the filename. For example, file1.txt located in //moose/dir/file1.txt: returns ftp://moose/dir/.

See also

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