64,678
社区成员
发帖
与我相关
我的任务
分享
//查找目录dir下的次级目录(不嵌套),填充到outList中
void searchFileInDirectroy( const std::string& dir, std::vector<std::string>& outList )
{
WIN32_FIND_DATA findData;
HANDLE hHandle;
std::string filePathName;
std::string fullPathName;
filePathName = dir;
filePathName += "\\*.*";
hHandle = FindFirstFile( filePathName.c_str(), &findData );
if( INVALID_HANDLE_VALUE == hHandle )
{
return;
}
do
{
if( strcmp(".", findData.cFileName) == 0 || strcmp("..", findData.cFileName) == 0 )
{
continue;
}
fullPathName = dir;
fullPathName += "\\";
fullPathName += findData.cFileName;
if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) // 是目录
{
outList.push_back(fullPathName);
}
}
while( FindNextFile( hHandle, &findData ) );
FindClose( hHandle );
}
#include <windows.h>
#include <stdio.h>
#include <string.h>
#ifdef UNICODE
#define PATHFORFIND L"D:/f/zl/"
void Findfiles(TCHAR* path)
{
HANDLE h;
WIN32_FIND_DATA fd;
TCHAR tmp_path[MAX_PATH];
TCHAR tmp_blank[MAX_PATH] = {0};
static int blank = 0;
blank++;
int i=1;
for(;i<blank;i++)
{
wcscat(tmp_blank,L" ");
}
swprintf(tmp_path,L"%s/*",path);
h = FindFirstFileW(tmp_path,&fd);
if (h==INVALID_HANDLE_VALUE)
{
blank--;
return;
}
do{
swprintf(tmp_path,L"%s%s%s/%s\n",tmp_blank,tmp_blank,path,fd.cFileName);
if(wcscmp(fd.cFileName,L".")==0 ||wcscmp(fd.cFileName,L"..")==0 )//deal with . and ..
continue;
swprintf(tmp_path,L"%s%s",tmp_blank,fd.cFileName);
if(fd.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN)
wcscat(tmp_path,L"--(隐藏)");
wcscat(tmp_path,L"\n");
wprintf(tmp_path);
if( fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY )
{
swprintf(tmp_path,L"%s/%s",path,fd.cFileName);
Findfiles(tmp_path);
}
}while(FindNextFileW(h,&fd));
blank--;
return;
}
int main()
{
Findfiles(PATHFORFIND);
return 0;
}