(1)枚举当前目录下所有的文件
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile("*.*", &fd);
if ( hFind != INVALID_HANDLE_VALUE )
{
do{
if ( !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{//非目录,即文件
cout << fd.cFileName << endl;
}
}while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
(2)枚举当前目录下所有的一级子目录
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile("*.*", &fd);
if ( hFind != INVALID_HANDLE_VALUE )
{
do{
if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )//目录
{
cout << fd.cFileName << endl;
}
}while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
(3)枚举当前目录下的所有子目录,递归调用
::SetCurrentDirectory(".");//当前目录
//如果枚举C:\下的所有子目录,包括子目录的子目录,则用
// ::SetCurrentDirectory("C:\\");
EnumFolders();
//-----------------------------------------------------------------------
void EnumFolders()
{
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile("*.*", &fd);
if ( hFind != INVALID_HANDLE_VALUE )
{
do {
if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
CString strName = fd.cFileName;
if ( strName != "." && strName != ".." )
{
cout << strName << endl;
::SetCurrentDirectory(fd.cFileName);
EnumFolders();
::SetCurrentDirectory("..");
}
}
}while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
}
阅读(1594) | 评论(0) | 转发(0) |