windows遍历文件夹

来源:互联网 发布:网络代理地址 编辑:程序博客网 时间:2024/05/17 04:04
#include <windows.h>
#include <stdio.h>


typedef BOOL (WINAPI *EnumerateFunc) (LPCSTR lpFileOrPath, void* pUserData);


void doFileEnumeration(LPSTR lpPath, BOOL bRecursion, BOOL bEnumFiles, EnumerateFunc pFunc, void* pUserData)
{


static BOOL s_bUserBreak = FALSE;
try{
//-------------------------------------------------------------------------
if(s_bUserBreak) return;


int len = strlen(lpPath);
if(lpPath==NULL || len<=0) return;


//NotifySys(NRS_DO_EVENTS, 0,0);


char path[MAX_PATH];
strcpy(path, lpPath);
if(lpPath[len-1] != '\\') strcat(path, "\\");
strcat(path, "360*");


WIN32_FIND_DATA fd;
HANDLE hFindFile = FindFirstFile(path, &fd);
if(hFindFile == INVALID_HANDLE_VALUE)
{
::FindClose(hFindFile); return;
}


char tempPath[MAX_PATH]; BOOL bUserReture=TRUE; BOOL bIsDirectory;


BOOL bFinish = FALSE;
while(!bFinish)
{
strcpy(tempPath, lpPath);
if(lpPath[len-1] != '\\') strcat(tempPath, "\\");
strcat(tempPath, fd.cFileName);


bIsDirectory = ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);


//如果是.或..
if( bIsDirectory
&& (strcmp(fd.cFileName, ".")==0 || strcmp(fd.cFileName, "..")==0))
{
bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
continue;
}


if(pFunc && bEnumFiles!=bIsDirectory)
{
bUserReture = pFunc(tempPath, pUserData);
if(bUserReture==FALSE)
{
s_bUserBreak = TRUE; ::FindClose(hFindFile); return;
}
}


//NotifySys(NRS_DO_EVENTS, 0,0);


if(bIsDirectory && bRecursion) //是子目录
{
doFileEnumeration(tempPath, bRecursion, bEnumFiles, pFunc, pUserData);
}


bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
}


::FindClose(hFindFile);


//-------------------------------------------------------------------------
}catch(...){ return; }
}


BOOL WINAPI myEnumerateFunc(LPCSTR lpFileOrPath, void* pUserData)
{
//char* pdot;
//if((pdot = strrchr(lpFileOrPath, '.')) && stricmp(pdot, ".mp3") == 0)
//{
printf("%s\n", lpFileOrPath);
//}
return TRUE;
}


int main()
{
doFileEnumeration("E:\\360Chrome\\Chrome\\User Data\\Default", TRUE, TRUE, myEnumerateFunc, NULL);
return 0;
}
0 0
原创粉丝点击