64,637
社区成员
发帖
与我相关
我的任务
分享
vector<USER_DEFINE_FIND_DATA> CFileManage::Win32RecurseDirectory(TCHAR* strDirectoryName)
{
TCHAR *strPathName = new TCHAR[BUFSIZE];
TCHAR *strTempPath = new TCHAR[BUFSIZE];
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE;
DWORD dwPos = 0;
USER_DEFINE_FIND_DATA Filedata;
ZeroMemory(&Filedata,sizeof(Filedata));
HANDLE hFindFile = INVALID_HANDLE_VALUE;
//save current path to a strPathName variable
_tcscpy(strPathName,strDirectoryName);
//add a \ to the end of strPathName
if (strPathName[_tcslen(strPathName)-1] != _T('\\'))
{
_tcscat(strPathName,_T("\\"));
}
//add a * to the end of strPathName
_tcscat(strPathName,_T("*.*"));
//find the first file in the directory,if succeeds,return value is a search handle used in a subsequent call to FindNextFile or FindClose.
bSearchFile = (INVALID_HANDLE_VALUE !=(hFindFile = FindFirstFile(strPathName,&Filedata)));
if (bSearchFile)
{
//if direction is current or subdirectories ,ignore it
if (_tcscmp(Filedata.cFileName,_T(".")) && _tcscmp(Filedata.cFileName,_T("..")))
{
m_LogOutput.FormatOutput(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
//Continues a file search next file,recurse calls RecurseDirectory,until no file or direction left
if (FindNextFile(hFindFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && _tcscmp(Filedata.cFileName,_T(".")) && _tcscmp(Filedata.cFileName,_T("..")))
{
_tcscpy(strTempPath,strDirectoryName);
_tcscat(strTempPath,Filedata.cFileName);
_tcscat(strTempPath,_T("\\"));
m_LogOutput.FormatOutput(_T(" %s\n"),Filedata.cFileName);
_tcscpy(Filedata.strFilePath,strTempPath);
_tcscat(Filedata.strFilePath,Filedata.cFileName);
m_vFileInfo.push_back(Filedata);
Win32RecurseDirectory(strTempPath);
}
else if (_tcscmp(Filedata.cFileName,_T(".")) && _tcscmp(Filedata.cFileName,_T("..")))
{
m_LogOutput.FormatOutput(_T(" %s\n"),Filedata.cFileName);
_tcscpy(Filedata.strFilePath,strDirectoryName);
_tcscat(Filedata.strFilePath,Filedata.cFileName);
m_vFileInfo.push_back(Filedata);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;
}
}
}
}
else
{
m_LogOutput.FormatOutput(_T("Invalid File Handle. GetLastError reports %d\n"), GetLastError ());
}
delete []strPathName;
delete []strTempPath;
FindClose(hFindFile);
return m_vFileInfo;
}
#include <windows.h>
void main()
{
const char *command = "dir c:\\";
system(command);
system("pause");
}
typedef void (*ENUM_FILES_FUNCTION)(const TCHAR*, LPWIN32_FILE_ATTRIBUTE_DATA);
#define SYS_IS_DIRECTORY(f) (f & FILE_ATTRIBUTE_DIRECTORY)
#define SYS_IS_HIDDEN(f) (f & FILE_ATTRIBUTE_HIDDEN)
#define SYS_IS_READONLY(f) (f & FILE_ATTRIBUTE_READONLY)
#define SYS_IS_SYSTEM(f) (f & FILE_ATTRIBUTE_SYSTEM)
extern void EnumFiles(const TCHAR*, ENUM_FILES_FUNCTION);
void EnumFiles(const TCHAR* szSourceDir, ENUM_FILES_FUNCTION callback_fun)
{
if (!szSourceDir) return;
WIN32_FIND_DATA FindFileData;
WIN32_FILE_ATTRIBUTE_DATA FindFileAttr;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR currDir[MAX_PATH];
TCHAR Result[MAX_PATH];
_tcsncpy(currDir, szSourceDir, _tcslen(szSourceDir) + 1);
_tcsncat(currDir, _T("\\*"), 3);
hFind = FindFirstFile(currDir, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) return;
do
{
if (*FindFileData.cFileName == '.') continue;
::_stprintf(Result, _T("%s\\%s"), szSourceDir, FindFileData.cFileName);
if (callback_fun && GetFileAttributesEx(Result, GetFileExInfoStandard, &FindFileAttr))
{
(*callback_fun)(Result, &FindFileAttr);
}
} while (FindNextFile(hFind, &FindFileData));
}