编写程序遍历文件夹及其子文件夹下所有文件,并输出到标准输出流或者文件流。

1. 先考虑在单层目录下,遍历所有文件。以C:\WINDOWS为例:

用到数据结构_finddata_t,文件信息结构体的指针。

struct _finddata_t
{
unsigned attrib; //文件属性
time_t time_create; //文件创建时间
time_t time_access; //文件上一次访问时间
time_t time_write; //文件上一次修改时间
_fsize_t size; //文件字节数
char name[_MAX_FNAME]; //文件名
};

文件属性是无符号整数,取值为相应的宏:_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的赋值来实现,例如要遍历C:\WINDOWS下的所有.exe文件

bool transfer(string fileName = "C:\\Windows\\*.exe", int exeNum = 0)
{
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo);

if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
return false;
}

do
{
exeNum ++;
cout << fileInfo.name <<endl;
} while (_findnext(handle, &fileInfo) == 0);
cout << " .exe files' number: " << exeNum << endl;

return true;
}

bool transfer(string fileName = "C:\\Windows\\*.exe", int exeNum = 0)
{
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo); if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
return false;
} do
{
exeNum ++;
cout << fileInfo.name <<endl;
} while (_findnext(handle, &fileInfo) == 0);
cout << " .exe files' number: " << exeNum << endl; return true;
}

2. 遍历文件夹及其子文件夹下所有文件。操作系统中文件夹目录是树状结构,使用深度搜索策略遍历所有文件。用到_A_SUBDIR属性,可运行程序如下:

void dfsFolder(string folderPath, ofstream &fout)
{
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
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 = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else
{
fout << folderPath << "\\" << FileInfo.name << " ";
}
}while (_findnext(Handle, &FileInfo) == 0);

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

void dfsFolder(string folderPath, ofstream &fout)
{
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
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 = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else
{
fout << folderPath << "\\" << FileInfo.name << " ";
}
}while (_findnext(Handle, &FileInfo) == 0); _findclose(Handle);
fout.close();
}

在判断有无子目录的if分支中,由于系统在进入一个子目录时,匹配到的头两个文件(夹)是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况。当需要对遍历到的文件做处理时,在else分支中添加相应的代码就好

C++下遍历文件夹的更多相关文章

  1. Delphi下遍历文件夹下所有文件的递归算法

    {------------------------------------------------------------------------------- 过程名:    MakeFileLis ...

  2. VC下遍历文件夹中的所有文件的几种方法

    一.使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #inclu ...

  3. windows下遍历文件夹

    Github地址 函数: HANDLE WINAPI FindFirstFile( _In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFil ...

  4. windows下遍历文件夹下的文件

    #include <io.h>#include <stdio.h>#include <iostream>using namespace std;int ReadSt ...

  5. VC.遍历文件夹中的文件

    1.VC下遍历文件夹中的所有文件的几种方法 - 年少要轻狂 - CSDN博客.html(https://blog.csdn.net/wllmsdn/article/details/27220999) ...

  6. C#遍历文件夹下所有文件

    FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...

  7. C# 遍历文件夹下所有子文件夹中的文件,得到文件名

    假设a文件夹在F盘下,代码如下.将文件名输出到一个ListBox中using System.Data;using System.Drawing;using System.Linq;using Syst ...

  8. python遍历文件夹下的文件

    在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...

  9. JAVA 遍历文件夹下的所有文件

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

随机推荐

  1. vs2015发布网站至azure web应用服务

    进入www.azure.cn管理门户 1,左下角新建web应用,实例如下,url设置为demo(有防止重名判断) 2,进入demo配置页(左侧web应用下点击demo) 3,demo首页,下载配置文件 ...

  2. hydra(九头蛇)多协议暴力破解工具

    一.简介 hydra(九头蛇)全能暴力破解工具,是一款全能的暴力破解工具,使用方法简单 二.使用 使用hydra -h 查看基本用法 三.命令 hydra [[[-l LOGIN|-L FILE] [ ...

  3. SQLServer学习-- Microsoft SQL Server 2008 Management Studio Express

    Microsoft SQL Server 2008 Management Studio Express is a free, integrated environment for accessing, ...

  4. win7 iis7 ftp配置

    1.安装ftp服务器 开始菜单找到控制面板>在左侧找到打开或关闭windows功能点击 弹出如下对话框, 在弹出的对话框中找到Internet信息服务,如下 然后如上图所示,选中ftp服务器复选 ...

  5. 编写高质量代码改善C#程序的157个建议——建议142:总是提供有意义的命名

    建议142:总是提供有意义的命名 除非有特殊原型,否则永远不要为自己的代码提供无意义的命名. 害怕需要过长的命名才能提供足够的意义?不要怕,其实我们更介意的是在代码的时候出现一个iTemp. int ...

  6. Alpha冲刺(六)

    Information: 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Details: 组员1(组长)柯奇豪 - 过去两天完成了哪些任务 1. 基于ssm框架的前后端交互 ...

  7. NSArray去除重复元素

    直接上代码吧! 1.可以创建一个新的数组,对需要去除重复的数组进行遍历,如果新数组不包含就数组,那么添加元素,如果包含就不添加. NSMutableArray *array = [NSMutableA ...

  8. alpha七天冲刺计划-第一天

    alpha七天冲刺计划-第一天 一.团队会议: 内容:具体分配任务到个人,预估项目时间,确定UI样式. 合照: 二.团队成员任务认领: 李尤:界面UI设计. 彭迪彬:HTML+CSS部分实现. 任路乾 ...

  9. Vc6.0 编译发生致命链接错误 :不能打开exe的文件

    错误: fatal error LNK1104: cannot open file "Debug/CeshiToolBar1.exe" 解决方法:打开任务管理器,找到对应的exe应 ...

  10. [LeetCode 题解]: Interger to Roman

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an i ...