c++17 filesystem, regex 遍历目录
| |
| #include <stdio.h> |
| |
| #include <sys/types.h> |
| #include <dirent.h> |
| #include <sys/stat.h> |
| |
| #include <sys/time.h> |
| |
| |
| #include <iostream> |
| #include <string> |
| #include <vector> |
| using namespace std; |
| |
| |
| |
| |
| std::vector<string> vecFilePath; |
| std::vector<string> vecFilename; |
| |
| |
| int isDirectory(string fullpath) |
| { |
| struct stat st; |
| int ret = stat(fullpath.c_str(), &st); |
| if(ret==0) |
| { |
| if(S_ISDIR( st.st_mode)) |
| { |
| |
| return 1; |
| } |
| else if(S_ISREG (st.st_mode) ) |
| { |
| |
| return 0; |
| } |
| } |
| |
| return -1; |
| } |
| |
| |
| |
| |
| |
| int TraverseDir(string path, bool isNeedFilterFlag, vector<string> vecSuffix,bool isTraveSubDirFlag) |
| { |
| DIR *d; |
| struct dirent *file; |
| |
| if(!(d = opendir(path.c_str()))) |
| { |
| cout<< "error opendir:"<<path<<endl; |
| return -1; |
| } |
| |
| while((file = readdir(d)) != NULL) |
| { |
| string strFileName(file->d_name); |
| |
| |
| |
| |
| |
| if(strFileName.compare(".")==0 || strFileName.compare("..")==0) |
| { |
| continue; |
| } |
| |
| string fullPath = path + "/" + strFileName; |
| |
| int ret = isDirectory(fullPath); |
| if(ret==0) |
| { |
| if(isNeedFilterFlag==true) |
| { |
| |
| bool isNeedSuffix(0); |
| for(uint32_t i =0; i< vecSuffix.size(); i++) |
| { |
| string fileSuffix = fullPath.substr(fullPath.length() - vecSuffix.at(i).length()); |
| bool bMatch = fileSuffix.compare(vecSuffix.at(i))==0; |
| isNeedSuffix = isNeedSuffix || bMatch; |
| } |
| |
| if(isNeedSuffix) |
| { |
| vecFilename.push_back(strFileName); |
| |
| } |
| } |
| else |
| { |
| vecFilename.push_back(strFileName); |
| } |
| } |
| else if(ret==1) |
| { |
| if(isTraveSubDirFlag==true) |
| { |
| TraverseDir(fullPath, isNeedFilterFlag, vecSuffix, isTraveSubDirFlag); |
| } |
| |
| |
| } |
| else |
| { |
| continue; |
| } |
| |
| } |
| closedir(d); |
| return 0; |
| } |
| int main() |
| { |
| string strPath("/home/scott/project"); |
| |
| vector<string> vecSuffix; |
| vecSuffix.push_back(".jpg"); |
| vecSuffix.push_back(".png"); |
| |
| vecSuffix.push_back(".aac"); |
| vecSuffix.push_back(".mp3"); |
| vecSuffix.push_back(".wma"); |
| vecSuffix.push_back(".wave"); |
| |
| vecSuffix.push_back(".mp4"); |
| vecSuffix.push_back(".rmvb"); |
| vecSuffix.push_back(".rmvb"); |
| vecSuffix.push_back(".mov"); |
| vecSuffix.push_back(".flv"); |
| vecSuffix.push_back(".ts"); |
| |
| |
| |
| bool isNeedFilterFlag(true); |
| bool isTraveSubDirFlag(true); |
| |
| |
| struct timeval tvBegin,tvEnd; |
| struct timezone tz; |
| |
| gettimeofday (&tvBegin , &tz); |
| TraverseDir(strPath, isNeedFilterFlag, vecSuffix, isTraveSubDirFlag); |
| gettimeofday (&tvEnd , &tz); |
| |
| |
| uint64_t SpendTime = (tvEnd.tv_sec-tvBegin.tv_sec)*1000+(tvEnd.tv_usec-tvBegin.tv_usec)/1000; |
| |
| std::cout<< "tvBegin.tv_sec ="<<tvBegin.tv_sec << ".tvBegin.tv_usec ="<< tvBegin.tv_usec<<std::endl; |
| std::cout<< "tvEnd.tv_sec ="<<tvEnd.tv_sec << ".tvEnd.tv_usec ="<< tvEnd.tv_usec <<std::endl; |
| std::cout<< "SpendTime="<<SpendTime<<"ms"<<std::endl; |
| |
| |
| cout<<"find number:"<<vecFilename.size()<<endl; |
| |
| |
| |
| |
| |
| |
| |
| |
| return 0; |
| } |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)