搜索目录下所有文件
2013-09-16 14:58
148 查看
1、Windows API提供了两个相关函数,FindFirstFile 和FindNextFile,使用它们写一个递归函数。
//1、Windows API提供了两个相关函数,FindFirstFile 和FindNextFile,使用它们写一个递归函数。//
//文件名:searchAllFile.cpp
// * int searchAllFile(string filePath, //要搜索的文件路径
// * intlayer //layer==0 //搜索的层次,输入路径的层次应该为0
// * stringfileNameToSave); //存储文件信息的文件名,包括路径
// *---------------------------------------------------------------------------------------------------------------------------------------
//
//
// * <io.h>中定义了结构体 struct_finddata64i32_t (C风格),用来存储文件的各种信息
// *详细如下:
// *struct _finddata64i32_t
// * {
// * unsigned attrib;
// * __time64_t time_create; // -1 for FAT file systems
// * __time64_t time_access; // -1 for FAT file systems
// * __time64_t time_write;
// * _fsize_t size;
// * char name[260];
// * };
// *各参数意义如下:
// *unsigned attrib :4个字节,存储文件的属性
// * _A_ARCH (存档) 0x20
// * _A_SUBDIR(文件夹)0x10
// * _A_SYSTEM(系统)0x04
// * _A_HIDDEN(隐藏)0x02
// * _A_RDONLY(只读)0x01
// * _A_NORMAL(正常)0X00
// *这些都是<io.h>中定义的宏,每一个都是一个unsigned int,各属性叠加时进行或运算,如_A_HIDDEN | _A_RDONLY
// *
// *__time64_t time_create: 文件创建的时间
// *__time64_t time_access: 文件最后一次访问的时间
// *__time64_t time_write: 文件最后以此修改的时间
// *_fsize_t size: 文件的大小,字节为单位
// *char name[260]: 文件名
// *---------------------------------------------------------------------------------------------------------------------------------------
//
// *<io.h> 中定义了两个函数
// *long _findfirst64i32(const char *_Filename,struct _finddata64i32_t * _FindData); ,查找第一个_Filename的信息,存储到结构体_FindData中
// * 查找成功,返回一个用于继续查找的句柄(一个唯一的编号)
// * 查找 失败,返回-1
// *int _findnext64i32(long handle,struct_finddata64i32_t *fileinfo) ; 根据句柄handle查找下一个文件,存放在 fileinfo中
// * 查找成功,返回0,失败返回-1
// *ing _findclose(long handle); 关闭句柄handle,成功返回0,失败返回-1
// *---------------------------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<io.h> //定义了结构体struct _finddata64i32_t(该结构体包含文件的相关属性,如文件名,文件的属性等
//定义函数: long _findfirst64i32(char* fileName,struct_finddata64i32_t *fileinf0);
//定义函数: int _findnext64i32(long handle,struct_finddata64i32_t *fileinfo);
//定义函数: int _findclose(long handle);
using namespace std;
//定义链表结点
struct fileInfoNode
{
struct _finddata64i32_t fileInfo; //保存文件信息的结构体
string fileName;
struct fileInfoNode* left;
};
//把文件信息连接到链表head中
int saveToLink(struct fileInfoNode*& head, //链表的头结点,引用参量
const string& fileName, //IN:文件名(包括路径)
const struct _finddata64i32_t& fileInfo)//IN:文件信息结构体,引用参量
{
//建立一个结点
fileInfoNode* p;
p=new fileInfoNode;
p->fileInfo=fileInfo; //把传入的文件信息复制进结点
p->fileName=fileName;
p->left=head;
head=p;
return 0;
}
//显示整个查找到的文件的信息到窗口
void displayLink(struct fileInfoNode* head)//IN:头结点,值传递参数
{
while(head!=NULL)
{
cout<<"fileName:"<<head->fileName<<endl;
cout<<"fileSize:"<<dec<<head->fileInfo.size<<"字节"<<endl;
cout<<"fileAttrib:"<<"0x"<<hex<<head->fileInfo.attrib<<endl;
cout<<"-------------------------------------------------------------------------------------------"<<endl;
head=head->left;
}
}
//把文件信息存储到文件saveFileName中
void saveLinkToFile(struct fileInfoNode* head,string saveFileName,int counter)
{
ofstream fout;
//打开文件
fout.open(saveFileName.c_str());
if((fout.is_open())==false)
{
cout<<"存储文件打开失败!"<<endl;
exit(-1);
}
fout<<"the file number is:"<<counter<<endl;
fout<<"-------------------------------------------------------------------------------------------------------"<<endl;
while(head!=NULL)
{
fout<<"fileName:"<<head->fileName<<endl;
fout<<"fileSize:"<<dec<<head->fileInfo.size<<"字节"<<endl;
fout<<"fileAttrib:"<<"0x"<<hex<<head->fileInfo.attrib<<endl;
fout<<"-------------------------------------------------------------------------------------------------------"<<endl;
head=head->left;
}
//关闭文件
fout.close();
}
//
int searchAllFile(string filePath,//IN:文件所在的路径,如:f:\example
int layer,//层次,只有层次为0时,才完成链表中文件信息的显示和存储
string fileInfoOut) //IN:存储的文件名
{
struct _finddata64i32_t fileInfo;//保存文件信息的结构体
static fileInfoNode* head=NULL;//fileInfoNode链表的头结点,静态存储
static int counter=0; //记录文件数目
long handle;//句柄
int done;//查找nextfile是否成功
string fileName=filePath+"\\*.*";//要搜索的文件名
//查找第一个文件,返回句柄
handle=_findfirst64i32(fileName.c_str(),&fileInfo);
if(handle==-1)
{
cout<<"该目录为空!"<<endl;
//cin.get();
return -1;
}
do
{
// cout<<"查找成功"<<endl;
// cin.get();
// cout<<fileInfo.name<<endl;
//如果是文件夹".",或者"..",则进行判断下一个文件
if((strcmp(fileInfo.name,".")==0)|(strcmp(fileInfo.name,"..")==0))
{
//cout<<"丢弃!"<<endl;
//cin.get();
continue;
}
//如果是文件夹,则进入下一层文件夹搜索
if((fileInfo.attrib&_A_SUBDIR)==_A_SUBDIR)
{
//cout<<"是文件夹"<<endl;
//cin.get();
string filePathSub=filePath+"\\"+fileInfo.name;
//递归调用
searchAllFile(filePathSub,++layer,fileInfoOut);
layer--;
}
//把搜集到的信息连接到文件
else
{
//cout<<"是文件,存储信息!"<<endl;
//cin.get();
counter++;
string fileNameTure=filePath+"\\"+fileInfo.name;
saveToLink(head,fileNameTure,fileInfo); //存储到链表中
}
}while(!(done=_findnext64i32(handle,&fileInfo)));
_findclose(handle);
//layer==时,完成链表的存储
if(layer==0)
{
//显示链表中的内容
displayLink(head);
//存储链表中的内容
saveLinkToFile(head,fileInfoOut,counter);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
searchAllFile("d:\\123", 0, "d:\\111.txt");
system("PAUSE");
return 0;
}
2、在MFC下要实现文件夹的递归遍历,可用CFileFind类,依次读取文件夹下的子文件夹和文件,并判断通过判断是文件夹还是文件来决定递归遍历(事实上,CFileFind本身还可以判断文件具体属于哪种类型,例如压缩文件、系统文件等)。另外要注意,遍历过程中会读到"."文件和".."文件,可通过fileFinder.IsDots()函数识别。
void CategoryTest(BayesTest* bt, CString tp) { CFileFind fileFinder; CString filePath = tp + _T("//*.*"); BOOL bFinished = fileFinder.FindFile(filePath); while(bFinished) //每次循环对应一个类别目录 { bFinished = fileFinder.FindNextFile(); if(fileFinder.IsDirectory() && !fileFinder.IsDots()) //若是目录则递归调用此方法 { CategoryTest(bt, fileFinder.GetFilePath()); } else //再判断是否为txt文件 { //获取文件类型 CString fileName = fileFinder.GetFileName(); int dotPos=fileName.ReverseFind('.'); CString fileExt=fileName.Right(fileName.GetLength()-dotPos); if(fileExt == _T(".txt")) //若是txt文件则开始分类测试 { CString category = fileName.Left(fileName.GetLength()-8); //应属类 CString result = bt->StartTest(fileFinder.GetFilePath()); //所分类 int n = m_ListCtrl.GetItemCount(); m_ListCtrl.InsertItem(n, fileName,0);// 插入为第n行第一列的内容:文件名 m_ListCtrl.SetItemText( n,1, category) ; // 设置第n行第列的内容:应属类 m_ListCtrl.SetItemText(n,2, result) ; // 设置第n行第列的内容:所分类 if(category == result) m_ListCtrl.SetItemText(n,3, _T("Correct!")) ; //判断正确 else m_ListCtrl.SetItemText(n,3, _T("Wrong!")) ; //判断错误 } } } fileFinder.Close(); }
相关文章推荐
- c++搜索目录下的所有文件
- java IO-搜索目录所有子文件、复制文件到指定地方
- 搜索目录下所有文件内容是否包含指定的关键词
- 利用Dataview实现搜索指定目录下的所有文件,以指定的条件排序(可以按文件名升降序,最后修改时间升降序)
- 用嵌套递归的方法搜索指定目录下的所有文件
- 使用grep指令在当前目录下所有文件中搜索指定文本
- dos下搜索当前目录及其子目录下的所有特定后缀名的文件并拷贝到新文件夹
- 搜索指定目录下的指定后缀名字的所有文件的内容
- 搜索指定目录下的所有文件或者指定文件(可用于多级目录)
- 搜索当前目录下所有文件包含某关键词的文本/文件名/行号
- grep搜索当前目录下的所有文件的内容
- find搜索目录下所有文件内容中的某个单词
- 假设知道所有的音乐文件都在XXX盘中,请从根目录开始搜索出所有的音乐文件
- shell: 搜索某目录下所有文件中的某关键词
- linux下c语言实现搜索根目录下所有文件
- UE4 C++实现搜索指定目录下的指定类型的所有文件
- linux下c语言实现搜索根目录下所有文件(转-wangxiangshang)
- 递归搜索给定的目录下所有的文件
- 如何使用C语言搜索指定目录下的所有文件?
- 递归搜索文件目录中的所有文件(vc)