http://www.microsoft.com/msj/archive/s1dcca.htm#fig1 Figure 1 FILESPEC.H #include <string.h> #include <stdio.h> #include <errno.h> #include <io.h>#ifndef BOOL typedef int BOOL; #endif////////////////// // Class to to navigate files that match a spec with wildcards // class CFileSpec : public _finddata_t { long m_hfile; // handle from findfirstpublic: CFileSpec() { m_hfile = -1; } ~CFileSpec() { if (m_hfile>=0) _findclose(m_hfile); } BOOL First(const char* filespec) { if (m_hfile>=0) _findclose(m_hfile); return (m_hfile=_findfirst((char*)filespec, this)) != -1; } BOOL Next() { return m_hfile>=0 && _findnext(m_hfile, this)==0; } };Figure 2 DIRLIST.CPP //////////////////////////////////////////////////////////////// // DIRLIST Copyright 1996 Microsoft Systems Journal. // If this program works, it was written by Paul DiLascia. // If not, I don't know who wrote it. // // DIRLIST illustrates how to fill a list box with long filenames. #include "stdafx.h" #include "resource.h" #include "filespec.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif//////////////////////////////////////////////////////////////// // Dialog class // class CMyDialog : public CDialog { CListBox m_lb1; CListBox m_lb2; public: CMyDialog(); virtual BOOL OnInitDialog(); DECLARE_MESSAGE_MAP() };BEGIN_MESSAGE_MAP(CMyDialog, CDialog) END_MESSAGE_MAP()CMyDialog::CMyDialog() : CDialog(IDD_DIALOG1) { }//////////////// // Initialize dialog: Subclass the edit controls // BOOL CMyDialog::OnInitDialog() { BOOL bRet = CDialog::OnInitDialog(); m_lb1.SubclassDlgItem(IDC_LIST1, this); m_lb2.SubclassDlgItem(IDC_LIST2, this); // Initialize list box #1 using DlgDirList char dir[128] = "*.*"; DlgDirList(dir, IDC_LIST1, 0, DDL_READWRITE); // Initialize list box #2 using CFileSpec CFileSpec spec; for (BOOL more=spec.First("*.*"); more; more=spec.Next()) { if (spec.attrib & (_A_SUBDIR|_A_HIDDEN|_A_SYSTEM)) continue; m_lb2.AddString(spec.name); } return bRet; }//////////////////////////////////////////////////////////////// // Application class
HANDLE fileHand;
CString fName;
int combo_I=0;
int file_fload=0;
fileHand=FindFirstFile(path+_T("*.*"),&fd);
fName=fd.cFileName;
do{
if(fd.dwFileAttributes==(DWORD)FILE_ATTRIBUTE_DIRECTORY)
{
m_Combo.InsertString(combo_I++,(LPCTSTR)fName);
}
}while(FindNextFile(fileHand,&fd));
Figure 1 FILESPEC.H #include <string.h>
#include <stdio.h>
#include <errno.h>
#include <io.h>#ifndef BOOL
typedef int BOOL;
#endif//////////////////
// Class to to navigate files that match a spec with wildcards
//
class CFileSpec : public _finddata_t {
long m_hfile; // handle from findfirstpublic:
CFileSpec() {
m_hfile = -1;
} ~CFileSpec() {
if (m_hfile>=0)
_findclose(m_hfile);
} BOOL First(const char* filespec) {
if (m_hfile>=0)
_findclose(m_hfile);
return (m_hfile=_findfirst((char*)filespec, this)) != -1;
} BOOL Next() {
return m_hfile>=0 && _findnext(m_hfile, this)==0;
}
};Figure 2 DIRLIST.CPP ////////////////////////////////////////////////////////////////
// DIRLIST Copyright 1996 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// DIRLIST illustrates how to fill a list box with long filenames.
#include "stdafx.h"
#include "resource.h"
#include "filespec.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif////////////////////////////////////////////////////////////////
// Dialog class
//
class CMyDialog : public CDialog {
CListBox m_lb1;
CListBox m_lb2;
public:
CMyDialog();
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
};BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
END_MESSAGE_MAP()CMyDialog::CMyDialog() : CDialog(IDD_DIALOG1)
{
}////////////////
// Initialize dialog: Subclass the edit controls
//
BOOL CMyDialog::OnInitDialog()
{
BOOL bRet = CDialog::OnInitDialog();
m_lb1.SubclassDlgItem(IDC_LIST1, this);
m_lb2.SubclassDlgItem(IDC_LIST2, this); // Initialize list box #1 using DlgDirList
char dir[128] = "*.*";
DlgDirList(dir, IDC_LIST1, 0, DDL_READWRITE); // Initialize list box #2 using CFileSpec
CFileSpec spec;
for (BOOL more=spec.First("*.*"); more; more=spec.Next()) {
if (spec.attrib & (_A_SUBDIR|_A_HIDDEN|_A_SYSTEM))
continue;
m_lb2.AddString(spec.name);
}
return bRet;
}////////////////////////////////////////////////////////////////
// Application class
Directory(char *DirName)//如"c:\\aaa"
{
CFileFind tempFind;
char tempFileFind[MAX_PATH];
sprintf(tempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[MAX_PATH];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
if(tempFind.IsDirectory())
{
char tempDir[MAX_PATH];
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
Directory(tempDir);
}
else
{
char tempFileName[MAX_PATH];
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
count++;
}
}
}
tempFind.Close();
}
可以查MSDN
CFileFind finder;
BOOL bWorking = finder.FindFile("*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
cout << (LPCTSTR) finder.GetFileName() << endl;
}
BOOL CopyMoveFiles(LPCTSTR lpSource, LPCTSTR lpDestination)
{
BOOL bResult = TRUE;
TCHAR szError[MAX_PATH];
CFileFind fileFind;
TCHAR szFolderFind[MAX_PATH];
TCHAR szSourceSubFolder[MAX_PATH];
TCHAR szDestinationSubFolder[MAX_PATH];
lstrcpy(szFolderFind,lpSource);
lstrcat(szFolderFind,"\\*.*");
BOOL bFind = fileFind.FindFile(szFolderFind);
while(bFind)
{
bFind = fileFind.FindNextFile();
if(fileFind.IsDots())
{
continue;
}
if(fileFind.IsDirectory())
{
lstrcpy(szSourceSubFolder,fileFind.GetFilePath());
lstrcpy(szDestinationSubFolder,lpDestination);
PathAppend(szDestinationSubFolder,fileFind.GetFileName());
if(PathFileExists(szDestinationSubFolder))
{
bResult = TRUE;
}
else
{
bResult = CreateDirectory(szDestinationSubFolder,NULL);
}
if(!bResult)
{
TCHAR szError[MAX_PATH];
lstrcpy(szError,IDS_FAILCREATE);
lstrcat(szError,szDestinationSubFolder);
ShowUpdateInfo(szError);
lstrcpy(m_szLastError,szError);
return FALSE;
}
if(!GetFolderSize(szSourceSubFolder))
{
continue;
}
bResult = CopyFolder(szSourceSubFolder,szDestinationSubFolder);
if(!bResult)
{
return FALSE;
}
}
else
{
TCHAR szSourceFile[MAX_PATH];
TCHAR szDestinationFile[MAX_PATH];
lstrcpy(szSourceFile,fileFind.GetFilePath());
lstrcpy(szDestinationFile,lpDestination);
PathAppend(szDestinationFile,fileFind.GetFileName());
m_ctrlProgress.StepIt();
SetFileAttributes(szSourceFile,FILE_ATTRIBUTE_NORMAL);
SetFileAttributes(szDestinationFile,FILE_ATTRIBUTE_NORMAL);
bResult = CopyFile(szSourceFile,szDestinationFile,FALSE);
if(!bResult)
{
return FALSE;
}
}
}
}