C++遍历文件夹目录的方法

这篇文章主要介绍了C++遍历文件夹目录的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、方法一:VS2019

 // dirlist.cpp : 定义控制台应用程序的入口点。 //#include "stdafx.h" #include  #include  #include  #include  using namespace std; /************************************************************************/ /* 获取文件夹下所有文件名 输入: path : 文件夹路径 exd :  所要获取的文件名后缀,如jpg、png等;如果希望获取所有 文件名, exd = ""或"*" 输出: files : 获取的文件名列表 shao, 20140707 */ /************************************************************************/ void getFiles(string path, string exd, vector& files) { //cout << "getFiles()" << path<< endl; //文件句柄 long  hFile = 0; //文件信息 struct _finddata_t fileinfo; string pathName, exdName; if (0 != strcmp(exd.c_str(), "")) { exdName = "\\*." + exd; } else { exdName = "\\*"; } if ((hFile = _findfirst(pathName.assign(path).append(exdName).c_str(), &fileinfo)) != -1) { do { //cout << fileinfo.name << endl; //如果是文件夹中仍有文件夹,迭代之 //如果不是,加入列表 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) getFiles(pathName.assign(path).append("\\").append(fileinfo.name), exd, files); } else { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) files.push_back(pathName.assign(path).append("\\").append(fileinfo.name)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } void main() { cout << "start list" << endl; vector files; const char* filePath = "D:\\opencv_4.1.0\\newbuild\\install\\x64\\vc16\\lib"; //获取该路径下的所有jpg文件 //getFiles(filePath, "jpg", files); //获取该路径下的所有lib文件 getFiles(filePath, "lib", files); //列表文件输出路径 FILE* fp; fopen_s(&fp, "d:\\dir_list.txt", "w"); int size = files.size(); for (int i = 0; i 

二、方法二:CMD

win+r调出“运行”窗口并输出cmd
输入:cd /d D:\opencv_4.1.0\newbuild\install\x64\vc16\lib 回车 (填自己的路径)
输入:dir /b *.lib *>0.txt 回车 

到此这篇关于C++遍历文件夹目录的方法的文章就介绍到这了,更多相关C++遍历文件夹目录内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

以上就是C++遍历文件夹目录的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言