/************************************************************************/
/* 时间:2014/12/17 19:44
目录深度遍历、目录广度遍历
LPCSTR 常量字符指针
与CString的相互转化
LPCSTR lpStr = "test";CString str(lpStr);
CString str(_T("test"));LPCSTR lp=(LPCSTR)str;
WIN32_FIND_DATA 记录文件相关属性的制度结构体 */
/************************************************************************/
#include
#include
#include
#define LEN 1024
BOOL DirectoryListExtend(LPCSTR Path); // 广度优先递归遍历目录中的所有文件
BOOL DirectoryListDepth(LPCSTR Path); // 深度优先递归遍历目录中所有的文件
char DirQueue[LEN][LEN];
void main()
{
DirectoryListExtend("D:");
}
BOOL DirectoryListExtend(LPCSTR Path)
{
printf("\n\nDirectoryListExtend\n%s\n",Path);
WIN32_FIND_DATA fd;
HANDLE hd;
int FileCount=0;
char FullPathName[LEN];
char FilePathName[LEN];
int DirCount=0;
strcpy(FilePathName, Path);
strcat(FilePathName, "\\*.*");
hd = FindFirstFile(FilePathName, &fd);
if(hd == INVALID_HANDLE_VALUE)
{
printf("搜索失败\n");
return 0;
}
while(::FindNextFile(hd, &fd))
{
// 过虑.和..
if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0 )
{
continue;
}
// 构造完整路径
wsprintf(FullPathName, "%s\\%s", Path,fd.cFileName);
FileCount++;
// 输出本级的文件
printf("\n%d %s ", FileCount, FullPathName);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("");
strcpy(DirQueue[DirCount],FullPathName);
DirCount++;
/*
printf("\n%s\n %d",DirQueue[DirCount-1],DirCount);*/
}
}
FindClose(hd);
while( --DirCount >= 0)
{
DirectoryListExtend(DirQueue[DirCount]);
}
return 0;
}
BOOL DirectoryListDepth(LPCSTR Path)
{
WIN32_FIND_DATA FindData;
HANDLE hError;
int FileCount = 0;
char FilePathName[LEN];
char FullPathName[LEN];
strcpy(FilePathName, Path);
strcat(FilePathName, "\\*.*");
hError = FindFirstFile(FilePathName, &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
printf("搜索失败!");
return 0;
}
while(::FindNextFile(hError, &FindData))
{
// 过虑.和..
if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0 )
{
continue;
}
// 构造完整路径
wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
FileCount++;
// 输出本级的文件
printf("\n%d %s ", FileCount, FullPathName);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("");
DirectoryListDepth(FullPathName);
}
}
return 0;
}