遍历盘符 控制遍历深度 C++

1.方式一

亲测有效

#include <iostream>
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <io.h>
#include <list>
#include<array>

using namespace std;

array<string, 20> arrayDisk;



std::string wstring_to_string(const std::wstring& ws)
{
	if (ws.empty())
	{
		return "";
	}

	// 获取待转换的数据的长度
	int len_in = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)ws.c_str(), -1, NULL, 0, NULL, NULL);
	if (len_in <= 0)
	{
		return "";
	}

	// 为输出数据申请空间
	std::string wstr_out;
	wstr_out.resize(len_in - 1, '\0');

	// 数据格式转换
	int to_result = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)ws.c_str(), -1, (LPSTR)wstr_out.c_str(), len_in, NULL, NULL);

	// 判断转换结果
	if (0 == to_result)
	{

		return "";
	}

	return wstr_out;

}

int GetAllDisk()
{
	//获取所有盘符
	int i = 0;
	
	DWORD dwLen = GetLogicalDriveStrings(0, NULL);//获取系统盘符字符串长度
	WCHAR *pszDriver = new WCHAR[dwLen];//构建字符数组
	GetLogicalDriveStrings(dwLen, pszDriver);//获取系统盘符字符串
	WCHAR* pDriver = pszDriver;
	while (*pDriver != '\0')
	{
		std::wcout << pDriver << std::endl;
		arrayDisk.at(i) = wstring_to_string(pDriver);
		int len = wcslen(pDriver);
		pDriver += wcslen(pDriver) + 1;//定位到下一个字符串,加1是为了跳过\0字符
		i++;
	}
	delete[] pszDriver;

	return i;
}


void GetSysDisk(string & sysDisk)
{
	//获取系统盘符
	WCHAR str[MAX_PATH];
	char outstr[20] = "";
	int i = 0;
	GetSystemDirectory(str, MAX_PATH);
	for(;i<3;i++)
		outstr[i] = str[i];
	sysDisk = outstr;

	std::cout << outstr << std::endl;
	
}


int n = 0;

list<string> lolPath;
list<string> wegamePath;

int dir_child(string path)
{
	long hFile = 0;
	struct _finddata_t fileInfo;
	string pathName, exdName, filepath;


	if ((hFile = _findfirst(pathName.assign(path).
		append("\\*").c_str(), &fileInfo)) == -1)
	{
		return 1;
	}

	do
	{

		string fname = string(fileInfo.name);

		if (fileInfo.attrib&_A_SUBDIR)
		{

			if (fname != ".." && fname != "." && (fname.find("$") == fname.npos))
			{
				cout << path.c_str() << "\\" << fileInfo.name << endl;
				dir_child(path + "\\" + fname);

			}

		}
		else
		{
			filepath = path + "\\" + fname;
			if (!fname.compare("Client.exe"))
			{
				lolPath.push_back(filepath);
			}
			if (!fname.compare("Wegame.exe"))
			{
				wegamePath.push_back(filepath);
			}
		}

	}while (_findnext(hFile, &fileInfo) == 0);
	_findclose(hFile);
	
	return 0;
}

int dir(string path)
{
	long hFile = 0;
	struct _finddata_t fileInfo;
	string pathName, exdName,filepath;
	

	if ((hFile = _findfirst(pathName.assign(path).
		append("\\*").c_str(), &fileInfo)) == -1) 
	{
		return 1;
	}

	if (n > 3)
	    return 0;
	n++;
	do 
	{
		
		if (fileInfo.attrib&_A_SUBDIR)
		{
			string fname = string(fileInfo.name);
			if (fname != ".." && fname != "." && (fname.find("$") == fname.npos))
			{
				filepath = path + "\\" + fname;
				if (filepath.find("英雄联盟") != filepath.npos)
				{
					dir_child(filepath);

				}
				if (filepath.find("WeGame") != filepath.npos)
				{
					dir_child(filepath);
				}
				cout << path.c_str() << "\\" << fileInfo.name << endl;
				dir(path + "\\" + fname);

			}

		}
		
	} while (_findnext(hFile, &fileInfo) == 0);
	_findclose(hFile);
	n--;
	return 0;
}


//template<typename Iter>
void list_elements(list<string> pathlist)
{
	auto begin = pathlist.begin();
	auto end = pathlist.end();
	while (begin != end)
	{

		std::cout<< (*begin).c_str() << std::endl;
		//printf("%s\n",begin);
		begin++;
	}
}

int main()
{
	int num = 0;
	num=GetAllDisk();
	int i = 0;
	string diskname;
	GetSysDisk(diskname);
	for(;i<num;i++)
	{
		
		if (arrayDisk[i] == diskname)
			continue;
		if (dir(arrayDisk[i]))
		{
			return 1;
		}
	}

	list_elements(lolPath);
	list_elements(wegamePath);

	system("pause");
}

 

2.方式二

未测试

#undef UNICODE
 
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <cstring>
 
#include <windows.h>
 
std::shared_ptr<std::vector<std::string> >
fileList(const std::string& folder_path)
{
	static std::shared_ptr<std::vector<std::string> > 
		folder_files(new std::vector<std::string>); //返回指针, 需要迭代使用
 
	WIN32_FIND_DATA FindData;
	HANDLE hError;
 
	int file_count(0);
	std::string file_path(folder_path); //路径名
	std::string full_file_path; //全路径名 
 
	file_path.append("/*.*");
	hError = FindFirstFile(file_path.c_str(), &FindData);
	if (hError == INVALID_HANDLE_VALUE) {
		std::cout << "failed to search files." << std::endl;
		return nullptr;
	}
	while(FindNextFile(hError, &FindData))
	{
		//过虑".", "..", "-q"
		if(0 == strcmp(FindData.cFileName, ".") || 
			0 == strcmp(FindData.cFileName, "..") || 
			0 == strcmp(FindData.cFileName, "-q"))
		{
			continue;
		}
 
		//完整路径
		full_file_path.append(folder_path);
		full_file_path.append("/");
		full_file_path.append(FindData.cFileName);
		++file_count;
 
		if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
			//std::cout << file_count << " " << full_file_path << "<Dir>" << std::endl;
			fileList(full_file_path);
		}else{
			folder_files->push_back(full_file_path);
			//std::cout << file_count << " " << full_file_path << std::endl;
		}
		full_file_path.clear(); //清空目录
	}
	return folder_files;
}
 
int main(void) 
{
	std::shared_ptr<std::vector<std::string> > folder_files;
	folder_files = fileList("E:/Picture/shoes3");
	if (folder_files) {
		for (size_t i=0; i != folder_files->size(); ++i) {
			std::cout << i+1 << " : " << (*folder_files)[i] << std::endl;
		}
	}
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值