c++获取目录下所有文件名windows

#pragma once
#include <iostream>
#include <string>
#include <vector>


class FindFiles
{
public:
	//completePath true:完整路径名 false:文件名
	//获取当前文件夹下所有文件名
	static std::vector<std::string> GetFileNames_OneLevel(std::string folder, bool completePath = false, std::string secName = ".*");
	//获取当前文件夹下所有文件夹名
	static std::vector<std::string> GetFolderNames_OneLevel(std::string folder, bool completePath = false, std::string secName = ".*");
	//获取文件夹下所有文件名
	static std::vector<std::string> GetFileNames_AllLevel(std::string folder, bool completePath = false, std::string secName = ".*");
	//获取文件夹下所有文件夹名
	static std::vector<std::string> GetFolderNames_AllLevel(std::string folder, bool completePath = false, std::string secName = ".*");
	//获取当前文件夹下所有文件和文件夹名
	static std::vector<std::string> GetNames_OneLevel(std::string folder, bool completePath = false, std::string secName = ".*");
	//获取文件夹下所有文件和文件夹名
	static std::vector<std::string> GetNames_AllLevel(std::string folder, bool completePath = false, std::string secName = ".*");


};

#include "FindFiles.h"
#include <Windows.h>
#include <direct.h>

using namespace std;

vector<string> FindFiles::GetFileNames_OneLevel(string folder, bool completePath, string secName)
{
	vector<string> fileNames;

	string szFind;
	string szFile;

	WIN32_FIND_DATAA FindFileData;

	szFind = folder;
	szFind = szFind + "\\*";
	szFind = szFind + secName;

	HANDLE hFind = ::FindFirstFileA(szFind.c_str(), &FindFileData);

	if (INVALID_HANDLE_VALUE == hFind)
	{
		cout << "Empty folder!" << endl;
		return vector<string>();
	}

	do
	{
		if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))  //如果不是目录
		{
			string file_name = FindFileData.cFileName;
			if (completePath)
				file_name = folder + "\\" + file_name;

			fileNames.push_back(file_name);
		}
	} while (::FindNextFileA(hFind, &FindFileData));

	::FindClose(hFind);
	return fileNames;
}

vector<string> FindFiles::GetFolderNames_OneLevel(string folder, bool completePath, string secName)
{
	vector<string> fileNames;
	string szFind;
	string szFile;

	WIN32_FIND_DATAA FindFileData;

	szFind = folder;
	szFind = szFind + "\\*";
	szFind = szFind + secName;

	HANDLE hFind = ::FindFirstFileA(szFind.c_str(), &FindFileData);

	if (INVALID_HANDLE_VALUE == hFind)
	{
		cout << "Empty folder!" << endl;
		return vector<string>();
	}

	do
	{
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  //如果是目录
		{
			if (FindFileData.cFileName[0] != '.')  //是目录,但不是.当前目录
			{
				string file_name = FindFileData.cFileName;
				if (completePath)
					file_name = folder + "\\" + file_name;

				fileNames.push_back(file_name);
			}
		}

	} while (::FindNextFileA(hFind, &FindFileData));

	::FindClose(hFind);
	return fileNames;
}

vector<string> FindFiles::GetFileNames_AllLevel(string folder, bool completePath, string secName)
{
	vector<string> fileNames;
	vector<string> level_fileNames = FindFiles::GetFileNames_OneLevel(folder,completePath,secName);
	if (!level_fileNames.empty())
	{
		fileNames.insert(fileNames.end(), level_fileNames.begin(), level_fileNames.end());
	}

	vector<string> level_folderNames = FindFiles::GetFolderNames_OneLevel(folder,true, secName);
	for (auto folderName : level_folderNames)
	{
		auto files = GetFileNames_AllLevel(folderName, completePath, secName);
		fileNames.insert(fileNames.end(), files.begin(), files.end());
	}
	return fileNames;
}

vector<string> FindFiles::GetFolderNames_AllLevel(string folder, bool completePath, string secName)
{
	vector<string> fileNames;
	vector<string> complete_folder_vecs;//完整路径名

	vector<string> level_folderNames = FindFiles::GetFolderNames_OneLevel(folder, completePath, secName);
	for (auto folderName : level_folderNames)
	{
		string complete_folder = folderName;
		if (!completePath)
			complete_folder = folder + "\\" + folderName;

		complete_folder_vecs.push_back(complete_folder);

		if(!completePath)
			fileNames.push_back(folderName);
		else
			fileNames.push_back(complete_folder);
	}

	for (auto folderName : complete_folder_vecs)
	{
		vector<string> folderNames = GetFolderNames_AllLevel(folderName, completePath, secName);
		fileNames.insert(fileNames.end(), folderNames.begin(), folderNames.end());
	}

	return fileNames;
}

vector<string> FindFiles::GetNames_OneLevel(string folder, bool completePath, string secName)
{
	vector<string> fileNames; 
	vector<string> level_fileNames = FindFiles::GetFileNames_OneLevel(folder, completePath, secName);
	if (!level_fileNames.empty())
	{
		fileNames.insert(fileNames.end(), level_fileNames.begin(), level_fileNames.end());
	}

	vector<string> level_folderNames = FindFiles::GetFolderNames_OneLevel(folder, completePath, secName);
	if (!level_folderNames.empty())
	{
		fileNames.insert(fileNames.end(), level_folderNames.begin(), level_folderNames.end());
	}

	return fileNames;
}

vector<string> FindFiles::GetNames_AllLevel(string folder, bool completePath, string secName)
{
	vector<string> fileNames;
	vector<string> level_names = GetNames_OneLevel(folder, completePath, secName);
	if (!level_names.empty())
	{
		fileNames.insert(fileNames.end(), level_names.begin(), level_names.end());
	}

	vector<string> level_folderNames = GetFolderNames_OneLevel(folder, true, secName);
	for (auto names : level_folderNames)
	{
		vector<string> level_next_names = GetNames_AllLevel(names, completePath, secName);
		if (!level_next_names.empty())
		{
			fileNames.insert(fileNames.end(), level_next_names.begin(), level_next_names.end());
		}
	}

	return fileNames;
}
#include "Code/FindFiles.h"

using namespace std;

int main()
{
	string folder = R"(D:\workspace\torch_server\deployment\train\segnet\segdata1)";

	vector<string> names0 = FindFiles::GetFileNames_OneLevel(folder, true);
	for (auto n : names0)
		cout << n << endl;
	cout << endl << endl;

	vector<string> names1 = FindFiles::GetFolderNames_OneLevel(folder, true);
	for (auto n : names1)
		cout << n << endl;
	cout << endl << endl;

	vector<string> names2 = FindFiles::GetFileNames_AllLevel(folder,true);
	for (auto n : names2)
		cout << n << endl;
	cout << endl << endl;

	vector<string> names3 = FindFiles::GetFolderNames_AllLevel(folder,true);
	for (auto n : names3)
		cout << n << endl;
	cout << endl << endl;


	vector<string> names4 = FindFiles::GetNames_OneLevel(folder,true);
	for (auto n : names4)
		cout << n << endl;
	cout << endl << endl;

	vector<string> names5 = FindFiles::GetNames_AllLevel(folder,true);
	for (auto n : names5)
		cout << n << endl;
	cout << endl << endl;



	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要批量获取指定目录下所有lib文件名并存储到文本中,可以使用以下方法: 1. 使用命令行工具 在Windows系统中,可以使用`dir`命令来列出指定目录下的所有文件,然后使用`findstr`命令来筛选出扩展名为.lib的文件,并将结果输出到文本中。例如,在命令行中输入以下命令: ``` dir /b "path/to/directory" | findstr /i "\.lib$" > "path/to/output.txt" ``` 其中,`/b`参数表示只显示文件名而不显示文件夹名,`/i`参数表示不区分大小写,`\`和`$`表示正则表达式中的转义字符,`>`表示将结果输出到文本文件中。 在Linux系统中,可以使用`ls`命令来列出指定目录下的所有文件,然后使用`grep`命令来筛选出扩展名为.lib的文件,并将结果输出到文本中。例如,在命令行中输入以下命令: ``` ls "path/to/directory" | grep "\.lib$" > "path/to/output.txt" ``` 其中,`\`和`$`表示正则表达式中的转义字符,`>`表示将结果输出到文本文件中。 2. 使用C++代码 可以使用C++的`<filesystem>`头文件来获取指定目录下的所有文件名,并筛选出扩展名为.lib的文件名,并将其写入文本中。以下是获取指定目录下所有lib文件名并存储到文本中的C++代码: ```cpp #include <iostream> #include <string> #include <fstream> #include <filesystem> namespace fs = std::filesystem; int main() { std::string dirPath = "path/to/directory"; // 指定目录路径 std::ofstream outFile("path/to/output.txt"); // 打开输出文件 for (const auto& entry : fs::directory_iterator(dirPath)) { if (entry.path().extension() == ".lib") // 判断文件扩展名是否为.lib { outFile << entry.path().filename().string() << std::endl; // 将文件名写入文件 } } outFile.close(); // 关闭输出文件 return 0; } ``` 你可以将上述代码中的`dirPath`变量和输出文件路径替换为你要获取lib文件的目录路径和输出文件的路径,然后编译运行即可。该程序将在指定目录下查找所有扩展名为.lib的文件,并将其文件名写入文本文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ListenAlone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值