C++遍历文件夹(包含子文件夹中的文件)

<span style="font-size:24px;">最近因为跟着老师做项目,需要遍历指定文件夹下的指定文件,故在此记录学习情况,以便以后复习使用。</span>

找了几种方法,最后决定使用数据结构_finddata_t ,网上有太多一样的信息了。。。

<span style="font-size:24px;">struct _finddata_t
{
    unsigned attrib;     //文件属性
    time_t time_create;  //文件创建时间
    time_t time_access;  //文件上一次访问时间
    time_t time_write;   //文件上一次修改时间
    _fsize_t size;  //文件字节数
    char name[_MAX_FNAME]; //文件名
};</span>


文件属性是无符号整数,取值为相应的宏:

_A_ARCH(存档),

_A_SUBDIR(文件夹),

_A_HIDDEN(隐藏),

_A_SYSTEM(系统),

_A_NORMAL(正常),

_A_RDONLY(只读)。


容易看出,通过这个结构体,我们可以得到关于该文件的很多信息。结合以下函数

我们可以将文件信息存储到这个结构体中:

//按FileName命名规则匹配当前目录第一个文件

_findfirst(_In_ const char * FileName, _Out_ struct _finddata64i32_t * _FindData); 

 //按FileName命名规则匹配当前目录下一个文件

_findnext(_In_ intptr_t _FindHandle, _Out_ struct _finddata64i32_t * _FindData);

 //关闭_findfirst返回的文件句柄

_findclose(_In_ intptr_t _FindHandle);

_findfirst 函数返回的是匹配到文件的句柄,数据类型为long。遍历过程可以指定文

件类型,这通过FileName的赋值来实现,例如要遍历D:\demo下的所有文


#include "stdafx.h"
#include <stdio.h>
#include <io.h>
#include "string"
#include "iostream"
using namespace std;

int main(void)
{
	_finddata_t FileInfo;
	string strfind = "d:\\demo\\*";
	long Handle = _findfirst(strfind.c_str(), &FileInfo);

	if (Handle == -1L)
	{
		cerr << "can not match the folder path" << endl;
		exit(-1);
	}
	do{
		//判断是否有子目录
		if (FileInfo.attrib & _A_SUBDIR)
		{
			//这个语句很重要
			if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
			{
				string newPath = "d:\\demo\\";
				newPath += FileInfo.name;
				//dfsFolder(newPath, fout);
				cout <<"Folder:		"<< newPath<<endl;
			}
		}
		else
		{
			//fout << folderPath << "\\" << FileInfo.name << " ";
			cout << "d:\\demo\\" << FileInfo.name<<endl;
		}
	} while (_findnext(Handle, &FileInfo) == 0);

	_findclose(Handle);
	//fout.close();

	return 0;
}

当然可以遍历特定文件,如遍历文本文件(.txt ) :   d:\\demo\\*.txt


_findfirst 函数返回的是匹配到文件的句柄,数据类型为long。遍历过程可以指定文

件类型,这通过FileName的赋值来实现,例如要遍历D:\\demo下的所有文


<pre name="code" class="cpp">#include "stdafx.h"
#include<iostream>   
#include<string>   
#include<io.h>  
using namespace std;

void   visit(string path, int layer)
{
	struct _finddata_t   filefind;
	string  curr = path + "\\*.*";
	int   done = 0, i, handle;
	if ((handle = _findfirst(curr.c_str(), &filefind)) == -1)
	{
		cerr << "can not match the folder path" << endl;
		exit(-1);
	}
	do{
		//判断是否有子目录
		if (_A_SUBDIR == filefind.attrib)
		{
			//这个语句很重要
			if ((strcmp(filefind.name, ".") != 0) && (strcmp(filefind.name, "..") != 0))
			{
				curr = path + "\\" + filefind.name;
				cout << "folder:			" <<curr << endl;
				<span style="color:#ff0000;">visit(curr, ++layer);</span>
			}
		}
		else
		{
			cout << path + "\\" + filefind.name << endl;
		}
	} while (_findnext(handle, &filefind) == 0);
	
	_findclose(handle);
}

int _tmain(int argc, _TCHAR* argv[])
{
	string   path;
	cout << "请输入目录" << endl;
	cin >> path;
	visit(path, 1);
	system("PAUSE");
	return 0;
}


 


在判断有无子目录的if分支中,由于系统在进入一个子目录时,匹配到的头两个文件

(夹)是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况。当需要对遍历到的

文件做处理时,在else分支中添加相应的代码就好




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值