地址: FindFile FindNextFile 如何遍历一个文件夹并获取里面文
如何遍历一个文件夹并获取里面文件的信息?蚊子132 今天给大家一段实现的代码,非常简单的。
- // 遍历文件夹,获取文件信息
- void TravelFolder(CString strDir)
- {
- // 文件当前目录
- TCHAR Buffer[MAX_PATH];
- DWORD dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
- CString strCurrent(Buffer);
- CFileFind filefind; //声明CFileFind类型变量
- CString strWildpath = strDir + _T("\\*.*"); //所有文件都列出。
- if(filefind.FindFile(strWildpath, 0)) //开始检索文件
- {
- BOOL bRet = TRUE;
- while(bRet)
- {
- bRet = filefind.FindNextFile(); //枚举一个文件
- if(filefind.IsDots()) //如果是. 或 .. 做下一个
- {
- continue;
- }
- // 文件名 begin
- CString strFileName = filefind.GetFileName();
- // 文件名 end
- // 文件修改时间 begin
- FILETIME filetime;
- FILETIME localtime;
- SYSTEMTIME systemtime;
- filefind.GetLastWriteTime(&filetime);
- FileTimeToLocalFileTime(&filetime, &localtime); //换成本地时间
- FileTimeToSystemTime(&localtime, &systemtime); //换成系统时间格式
- CString strTime = _T("");
- strTime.Format(_T("%04d%02d%02d%02d%02d%02d"),
- systemtime.wYear, systemtime.wMonth, systemtime.wDay,
- systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
- // 文件修改时间 end
- if(!filefind.IsDirectory()) //不是子目录,把文件名打印出来
- {
- CString strWrite = _T("");
- strWrite += strFileName;
- strWrite += _T("\t");
- strWrite += strTime;
- strWrite += + _T("\r\n");
- TRACE(strWrite);
- }
- else //如果是子目录,递归调用该函数
- {
- CString strNewDir = strDir + CString(_T("\\")) + filefind.GetFileName();
- TravelFolder(strNewDir);//递归调用该函数打印子目录里的文件
- }
- }
- filefind.Close();
- }
- }