CFileFind Overview
The MFC class CFileFind performs local file searches and is the base class forCGopherFileFind andCFtpFileFind, which perform Internet file searches.CFileFind includes member functions that begin a search, locate a file, and return the title, name, or path of the file. For Internet searches, the member functionGetFileURL returns the file’s URL.
CFileFind is the base class for two other MFC classes designed to search particular server types:CGopherFileFind works specifically with gopher servers, andCFtpFileFind works specifically with FTP servers. Together, these three classes provide a seamless mechanism for the client to find files, regardless of the server protocol, the file type, or location, on either a local machine or a remote server.
The following code will enumerate all the files in the current directory, printing the name of each file:
CFileFind finder;
BOOL bWorking = finder.FindFile("*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
cout << (LPCTSTR) finder.GetFileName() << endl;
}
To keep the example simple, this code uses the standard C++ library cout class. Thecout line could be replaced with a call toCListBox::AddString, for example, in a program with a graphical user interface.
For more information about how to use CFileFind and the other WinInet classes, see the articleInternet Programming with WinInet inVisual C++ Programmer's Guide.
#include <afx.h>
Class Member
CFileFind::FindFile
virtual BOOL FindFile( LPCTSTR pstrName = NULL, DWORDdwUnused = 0 );
Return Value
Nonzero if successful; otherwise 0. To get extended error information, call the Win32 functionGetLastError.
Parameters
pstrName
A pointer to a string containing the name of the file to find. If you passNULL forpstrName,FindFile does a wildcard (*.*) search.
dwUnused
Reserved to make FindFile polymorphic with derived classes. Must be 0.
Remarks
Call this member function to open a file search.
After calling FindFile to begin the file search, call FindNextFile to retrieve subsequent files. You must call FindNextFile at least once before calling any of the following attribute member functions:
- GetCreationTime
- GetFileName
- GetFileTitle
- GetFilePath
- GetFileURL
- GetLastAccessTime
- GetLastWriteTime
- GetLength
- GetRoot
CFileFind::FindNextFile
virtual BOOL FindNextFile( );
Return Value
Nonzero if there are more files; zero if this is the last file, and the previous call to eitherFindFile orFindNextFile returned nonzero. To get extended error information, call the Win32 functionGetLastError.
Remarks
Call this member function to continue a file search from a previous call to FindFile. You must call FindNextFile at least once before calling any of the following attribute member functions:
- GetCreationTime
- GetFileName
- GetFileTitle
- GetFilePath
- GetFileURL
- GetLastAccessTime
- GetLastWriteTime
- GetLength
- GetRoot
CFindFile::IsDirectory
BOOL IsDirectory( ) const;
Return Value
Nonzero if successful; otherwise 0.
Remarks
Call this member function to determine if the found file is a directory. A file that is a directory is marked with FILE_ATTRIBUTE_DIRECTORY a file attribute identified in theWIN32_FIND_DATA structure.
CFindFile::IsDots( )
virtual BOOL IsDots( ) const;
Return Value
Nonzero if the found file has the name "." or "..", which indicates that the found file is actually a diretory. Otherwise 0.
Remarks
Call this member function to test for the current directory and parent directory markers while iterating through files.
CFindFile::GetFileName()
virtual CString GetFileName( ) const;
Return Value
The name of the most-recently-found file.
Remarks
Call this member function to get the name of the found file. You must call FindNextFile at least once before calling GetFileName.
该函数会获取所寻找到文件的文件名。使用该函数前,你必须先至少使用一次FindNextFile()。
GetFileName is one of three CFileFind member functions that return some form of the file name. The following list describes the three and how they vary:
- GetFileName returns the file name, including the extension. For example, callingGetFileName to generate a user message about the file
c:\myhtml\myfile.txt
returns the file namemyfile.txt
.
- GetFilePath returns the entire path for the file. For example, callingGetFilePath to generate a user message about the file
c:\myhtml\myfile.txt
returns the file pathc:\myhtml\myfile.txt
.
- GetFileTitle returns the file name, excluding the file extension. For example, callingGetFileTitle to generate a user message about the file
c:\myhtml\myfile.txt
returns the file titlemyfile
.