http://blog.chinaunix.net/uid-1878979-id-2817063.html
2006
包括使用MFC的一个和没用MFC的一个。。// walk_path.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
#include
//#include
using namespace std;
vector fileVec;
vector fileVec_api;
bool walk_path(CString FolderPath);
bool walk_path_api(const char* folder);
ofstream path_file("path_file.txt");
CStdioFile out;
int main(int argc, char* argv[])
{
CString FolderPath = "D:\test_path\*";
char FolderPath_api[] = "D:\walk_path\*";
walk_path_api(FolderPath_api);
out.Open("out.txt",CFile::modeCreate|CFile::modeWrite);
if(!walk_path(FolderPath))
{
cout << "something error!" << endl;
}
out.Close();
return 0;
}
/*
bool walk_path(CString FolderPath)
{
CFileFind file_finder;
BOOL bfind = file_finder.FindFile(FolderPath);
CString path_temp;
if(bfind)
{
while(file_finder.FindNextFile())
{
path_temp = file_finder.GetFilePath();
string str = (LPCSTR)path_temp;
path_file << str.c_str() << endl;
//path_file << path_temp << endl;
cout << path_temp << endl;
out.WriteString(path_temp+"\n");
fileVec.push_back(path_temp);
if((!file_finder.IsDots()) && (file_finder.IsDirectory()))
{
walk_path(file_finder.GetFilePath() + "\*");
}
}
{
path_temp = file_finder.GetFilePath();
string str = (LPCSTR)path_temp;
path_file << str.c_str() << endl;
cout << path_temp << endl;
out.WriteString(path_temp+"\n");
fileVec.push_back(path_temp);
if((!file_finder.IsDots()) && (file_finder.IsDirectory()))
{
walk_path(file_finder.GetFilePath() + "\*");
}
}
}
return true;
}
*/
bool walk_path(CString FolderPath)
{
CFileFind file_finder;
BOOL bfind = file_finder.FindFile(FolderPath);
CString path_temp;
vector
fileVec_temp;
if(bfind)
{
//将该目录下的所有文件和文件夹放入fileVec_temp中.
while(file_finder.FindNextFile())
{
path_temp = file_finder.GetFilePath();
string str = (LPCSTR)path_temp;
path_file << str.c_str() << endl;
cout << str.c_str() << endl;
out.WriteString(path_temp+"\n");
fileVec_temp.push_back(path_temp);
}
{
path_temp = file_finder.GetFilePath();
string str = (LPCSTR)path_temp;
path_file << str.c_str() << endl;
cout << str.c_str() << endl;
out.WriteString(path_temp+"\n");
fileVec_temp.push_back(path_temp);
}//!
//将fileVec_temp中存放的非文件夹文件放入全局变量fileVec中,并递归调用walk_path遍历文件夹
//将fileVec_temp中存放的非文件夹文件放入全局变量fileVec中,并将其从fileVec_temp中删除
for(int i = 0; i < fileVec_temp.size(); i++)
{
path_temp = fileVec_temp.at(i);
fileVec.push_back(path_temp);
if(!(GetFileAttributes((LPCSTR)path_temp) && FILE_ATTRIBUTE_DIRECTORY)|( path_temp.Find("\.") != -1 ))
{
fileVec_temp.erase(&fileVec_temp.at(i));
i--;
}
}
//现在fileVec_temp 中存放的是文件夹.递归遍历该文件夹
for(int j = 0; j < fileVec_temp.size(); j++)
{
walk_path(fileVec_temp.at(j) + "\*");
}
}
return true;
}
bool walk_path_api(const char* folder)
{
string folder_temp_1 = folder;
int loc;
loc = folder_temp_1.find("\*");
folder_temp_1 = folder_temp_1.substr(0, loc+1);
string folder_temp_2 = folder_temp_1;
WIN32_FIND_DATA find_data;
HANDLE find_handle;
find_handle = FindFirstFile(folder, &find_data);
vector fileVec_temp;
fileVec_temp.push_back(folder_temp_2.append((string)find_data.cFileName));
folder_temp_2 = folder_temp_1;
BOOL bfind = TRUE;
if(bfind)
{
//将该目录下的所有文件和文件夹放入fileVec_temp中.
while(FindNextFile(find_handle, &find_data))
{
fileVec_temp.push_back(folder_temp_2.append((string)find_data.cFileName));
folder_temp_2 = folder_temp_1;
}
//将fileVec_temp中存放的非文件夹文件放入全局变量fileVec中,并递归调用walk_path遍历文件夹
//将fileVec_temp中存放的非文件夹文件放入全局变量fileVec中,并将其从fileVec_temp中删除
string path_temp;
for(int i = 0; i < fileVec_temp.size(); i++)
{
path_temp = fileVec_temp.at(i);
// cout << path_temp.c_str() << endl;
fileVec_api.push_back(path_temp);
if((!(GetFileAttributes(path_temp.c_str()) & FILE_ATTRIBUTE_DIRECTORY)) | (path_temp.find("\.") != -1))
{
fileVec_temp.erase(&fileVec_temp.at(i));
i--;
}
}
//现在fileVec_temp 中存放的是文件夹.递归遍历该文件夹
for(int j = 0; j < fileVec_temp.size(); j++)
{
string folder_new = fileVec_temp.at(j);
walk_path_api(folder_new.append("\*").c_str());
}
}
return true;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。