C++读取文件夹中所有文件的路径,包含子文件夹

C++读取文件夹中所有文件的路径,包含子文件夹

    C++实现指定文件夹的路径,获取该路径下所有文件的路径,含子文件夹的文件,为了实现跨平台编译,最后会给出Window和Linux的完成程序:
【1】下面是Windows版本:
#include <iostream>
#include <vector>
#include <cstring>        // for strcat()
#include <io.h>
using namespace std;

vector<char*>  getFilesList(const char * dir);
int main()
{

	char dir[200];
	cout << "Enter a directory: ";
	cin.getline(dir, 200);
	vector<char*>allPath= getFilesList(dir);

	cout << "输出所有文件的路径:" << endl;
	for (size_t i = 0; i < allPath.size(); i++)
	{
		char *perPath = allPath.at(i);
		cout << perPath <<endl;
	}

	return 0;
}

vector<char*> getFilesList(const char * dir)
{
	vector<char*> allPath;
	char dirNew[200];
	strcpy(dirNew, dir);
	strcat(dirNew, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dirNew, &findData);
	if (handle == -1) {// 检查是否成功
		cout << "can not found the file ... " << endl;
		return allPath;
	}      

	do
	{
		if (findData.attrib & _A_SUBDIR)//// 是否含有子目录
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;


			//cout << findData.name << "\t<dir>\n";
			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
			strcpy(dirNew, dir);
			strcat(dirNew, "\\");
			strcat(dirNew, findData.name);
			vector<char*> tempPath=getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小
		{
			char *filePath=new char[200];
			strcpy(filePath, dir);
			strcat(filePath, "\\");
			strcat(filePath, findData.name);
			allPath.push_back(filePath);
			//cout << filePath << "\t" << findData.size << " bytes.\n";

		}
	} while (_findnext(handle, &findData) == 0);
	_findclose(handle);    // 关闭搜索句柄
	return allPath;
}
输入文件夹的路径,会返回该路径下所有文件的路径,包含所有子文件的文件路径,并保存在vector容器中,下面是C++精简的版本
#include <vector>
#include <string>
#include <iostream> //for cout
#include <io.h>     //for _finddata_t、 _findnext
using namespace std;

vector<string>  getFilesList(string dir);
int main()
{

	char dir[200];
	cout << "Enter a directory: ";
	cin.getline(dir, 200);
	vector<string>allPath = getFilesList(dir);

	cout << "输出所有文件的路径:" << endl;
	for (size_t i = 0; i < allPath.size(); i++)
	{
		string  perPath = allPath.at(i);
		cout << perPath << endl;
	}

	return 0;
}

vector<string> getFilesList(string dir)
{
	vector<string> allPath;
	// 在目录后面加上"\\*.*"进行第一次搜索
	string dir2= dir + "\\*.*";

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dir2.c_str(), &findData);
	if (handle == -1) {// 检查是否成功
		cout << "can not found the file ... " << endl;
		return allPath;
	}
	do
	{
		if (findData.attrib & _A_SUBDIR)//// 是否含有子目录
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;

			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
			string dirNew = dir + "\\" + findData.name;
			vector<string> tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小
		{
			string filePath = dir + "\\" + findData.name;
			allPath.push_back(filePath);
			//cout << filePath << "\t" << findData.size << " bytes.\n";
		}
	} while (_findnext(handle, &findData) == 0);
	_findclose(handle);    // 关闭搜索句柄
	return allPath;
}
【2】下面是Linux版本:
#include <iostream>
#include <memory.h>
#include <dirent.h>
#include <vector>
using namespace std;

string gFileLoadPath = "/home/panjq/QTpro/QtOpenCV-qmake/myLib/";
vector<string> getFilesList(string dir);

int main()
{
    char dir[200];
    string Path;
    cout << "Enter a directory: ";
    cin.getline(dir, 200);
    vector<string>allFileName = getFilesList(dir);
    cout << "输出所有文件的路径:" << endl;
    for (size_t i = 0; i < allFileName.size(); i++)
     {
       string filename = allFileName.at(i);
       Path = filename;
       cout << Path << endl;
     }
     return 0;
}

vector<string> getFilesList(string dirpath){
    DIR *dir = opendir(dirpath.c_str());
    if (dir == NULL)
    {
        cout << "opendir error" << endl;
    }

    vector<string> allPath;
    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL)
    {
        if (entry->d_type == DT_DIR){//It's dir
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            string dirNew = dirpath + "/" + entry->d_name;
            vector<string> tempPath = getFilesList(dirNew);
            allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());

        }else {
            //cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
            string name = entry->d_name;
            string imgdir = dirpath +"/"+ name;
            //sprintf("%s",imgdir.c_str());
            allPath.push_back(imgdir);
        }

    }
    closedir(dir);
    //system("pause");
    return allPath;
}

【3】支持Windows和Linux的程序:

#include <iostream>
#include <string>  
#include <vector>
//#define linux
//#define _WIN32

using namespace std;
vector<string> getFilesList(string dir);

int main()
{
	char dir[200];
	cout << "Enter a directory: ";
	cin.getline(dir, 200);
	vector<string>allFileList = getFilesList(dir);
	cout << "输出所有文件的路径:" << endl;
	for (size_t i = 0; i < allFileList.size(); i++)
	{
		string filePath = allFileList.at(i);
		cout << filePath << endl;
	}
	return 0;
}

#ifdef linux
#include <memory.h>
#include <dirent.h>
vector<string> getFilesList(string dirpath) {
	vector<string> allPath;
	DIR *dir = opendir(dirpath.c_str());
	if (dir == NULL)
	{
		cout << "opendir error" << endl;
		return allPath;
	}
	struct dirent *entry;
	while ((entry = readdir(dir)) != NULL)
	{
		if (entry->d_type == DT_DIR) {//It's dir
			if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
				continue;
			string dirNew = dirpath + "/" + entry->d_name;
			vector<string> tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());

		}
		else {
			//cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
			string name = entry->d_name;
			string imgdir = dirpath + "/" + name;
			//sprintf("%s",imgdir.c_str());
			allPath.push_back(imgdir);
		}

	}
	closedir(dir);
	//system("pause");
	return allPath;
}
#endif

#ifdef _WIN32//__WINDOWS_
#include <io.h>  
vector<string> getFilesList(string dir)
{
	vector<string> allPath;
	// 在目录后面加上"\\*.*"进行第一次搜索
	string dir2 = dir + "\\*.*";

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dir2.c_str(), &findData);
	if (handle == -1) {// 检查是否成功
		cout << "can not found the file ... " << endl;
		return allPath;
	}
	while (_findnext(handle, &findData) == 0)
	{
		if (findData.attrib & _A_SUBDIR)//// 是否含有子目录
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;
			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
			string dirNew = dir + "\\" + findData.name;
			vector<string> tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小
		{
			string filePath = dir + "\\" + findData.name;
			allPath.push_back(filePath);
		}
	}
	_findclose(handle);    // 关闭搜索句柄
	return allPath;
}
#endif


************************************************************************************************************

单独获取全部的文件的名字
2017/09/21更新版
#include "stdafx.h"
#include <iostream>  
#include <vector>  
#include <cstring>        // for strcat()  
#include <io.h>  
using namespace std;

vector<char*>  getFilesNameList(const char * dir);
int main()
{

	char dir[200];
	char perFilePath[200];
	cout << "Enter a directory: ";
	cin.getline(dir, 200);
	vector<char*>allFileName = getFilesNameList(dir);

	cout << "输出所有文件的路径:" << endl;
	for (size_t i = 0; i < allFileName.size(); i++)
	{
		char *filename = allFileName.at(i);
		strcpy(perFilePath, dir);
		strcat(perFilePath, "\\");
		strcat(perFilePath, filename);
		cout << perFilePath << endl;
	}

	return 0;
}

vector<char*> getFilesNameList(const char * dir)
{
	vector<char*> allPath;
	char dirNew[200];
	strcpy(dirNew, dir);
	strcat(dirNew, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索  

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dirNew, &findData);
	if (handle == -1) {// 检查是否成功  
		cout << "can not found the file ... " << endl;
		return allPath;
	}

	do
	{
		if (findData.attrib & _A_SUBDIR)//// 是否含有子目录  
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索  
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;


			//cout << findData.name << "\t<dir>\n";  
			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索  
			strcpy(dirNew, dir);
			strcat(dirNew, "\\");
			strcat(dirNew, findData.name);
			vector<char*> tempPath = getFilesNameList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小  
		{
			char *filePath = new char[200];
			//strcpy(filePath, dir);
			//strcat(filePath, "\\");
			//strcat(filePath, findData.name);
			sprintf(filePath, "%s", findData.name);
			allPath.push_back(filePath);
			//cout << filePath << "\t" << findData.size << " bytes.\n";  

		}
	} while (_findnext(handle, &findData) == 0);
	_findclose(handle);    // 关闭搜索句柄  
	return allPath;
}
2017/12/12更新版
下面的代码,只是实现获得当前文件夹的所有文件的路径,但不能含有子文件夹
#include "stdafx.h"          
#include "stdafx.h"  
#include <iostream>    
#include <vector>    
#include <cstring>        // for strcat()    
#include <io.h>    
using namespace std;

vector<char*>  getFilesNameList(const char * dir);
int main()
{

	char dir[200];
	string Path;
	cout << "Enter a directory: ";
	cin.getline(dir, 200);
	vector<char*>allFileName = getFilesNameList(dir);
	cout << "输出所有文件的路径:" << endl;
	for (size_t i = 0; i < allFileName.size(); i++)
	{
		char *filename = allFileName.at(i);
		Path = dir+(string)"\\"+ filename;
		cout << Path.c_str() << endl;
	}

	return 0;
}

vector<char*> getFilesNameList(const char * dir)
{
	vector<char*> allPath;
	char dirNew[200];
	strcpy_s(dirNew, dir);
	strcat_s(dirNew, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索    

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dirNew, &findData);
	if (handle == -1) {// 检查是否成功    
		cout << "can not found the file ... " << endl;
		return allPath;
	}

	do
	{
		if (findData.attrib & _A_SUBDIR)//// 是否含有子目录    
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索    
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;


			//cout << findData.name << "\t<dir>\n";    
			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索    
			strcpy_s(dirNew, dir);
			strcat_s(dirNew, "\\");
			strcat_s(dirNew, findData.name);
			vector<char*> tempPath = getFilesNameList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小    
		{
			char *filePath = new char[200];
			//strcpy(filePath, dir);  
			//strcat(filePath, "\\");  
			//strcat(filePath, findData.name);  
			sprintf(filePath, "%s", findData.name);
			allPath.push_back(filePath);
			//cout << filePath << "\t" << findData.size << " bytes.\n";    

		}
	} while (_findnext(handle, &findData) == 0);
	_findclose(handle);    // 关闭搜索句柄    
	return allPath;
}

【3】bat脚本获得某个文件夹下所有的文件名:
    首先在你要得出文件名称下的文件夹下新建一个文本文档.txt。然后在里面输入  DIR *.*  /B >LIST.TXT。然后在把.txt格式改为 .bat格式。最后在双击该文件就能得到该文件夹下所有的文件名了,是不是很方便呢。
尊重原创,转载请注明出处http://blog.csdn.net/guyuealian/article/details/77981953


阅读更多

扫码向博主提问

去开通我的Chat快问

guyuealian

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 图像处理
  • AI人工智能
  • OpenCV
  • TensorFlow
  • Caffe
版权声明:本文为博主原创文章,未经博主允许不得转载(pan_jinquan) https://blog.csdn.net/guyuealian/article/details/77981953
个人分类: C / C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭