在此之前需要了解 WIN32_FIND_DATA的结构 以及  FindFirstFile、 FindNextFile原型以及用法注意事项传送门如下

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365740(v=vs.85).aspx

涉及的宏定义

------------------------------------------------------------------------------------------------------------------------------------------------

  • FILE_ATTRIBUTE_ARCHIVE
    32 (0x20)
A file or directory that is an archive file or directory. Applications typically use this attribute to markfiles for backup or removal.存档类
  • FILE_ATTRIBUTE_COMPRESSED
    2048 (0x800)
A file or directory that is compressed. For a file, all of the data in the file is compressed. For a directory, compression is the default for newly created files and subdirectories.
  • FILE_ATTRIBUTE_DEVICE
    64 (0x40)
This value is reserved for system use.驱动类
  • FILE_ATTRIBUTE_DIRECTORY
    16 (0x10)
The handle that identifies a directory.目录类
  • FILE_ATTRIBUTE_ENCRYPTED
    16384 (0x4000)
A file or directory that is encrypted. For a file, all data streams in the file are encrypted. For a directory, encryption is the default for newly created files and subdirectories.
  • FILE_ATTRIBUTE_HIDDEN
    2 (0x2)
The file or directory is hidden. It is not included in an ordinary directory listing.隐藏
  • FILE_ATTRIBUTE_INTEGRITY_STREAM
    32768 (0x8000)
The directory or user data stream is configured with integrity (only supported on ReFS volumes). It is not included in an ordinary directory listing. The integrity setting persists with the file if it's renamed. If a file is copied the destination file will have integrity set if either the source file or destination directory have integrity set.
Windows Server2008R2, Windows7, Windows Server2008, WindowsVista, Windows Server2003, and WindowsXP:This flag is not supported until Windows Server2012.
  • FILE_ATTRIBUTE_NORMAL
    128 (0x80)
A file that does not have other attributes set. This attribute is valid only when used alone.普通
  • FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
    8192 (0x2000)
The file or directory is not to be indexed by the content indexing service.
  • FILE_ATTRIBUTE_NO_SCRUB_DATA
    131072 (0x20000)
The user data stream not to be read by the background data integrity scanner (AKA scrubber). When set on a directory it only provides inheritance. This flag is only supported on Storage Spaces and ReFS volumes. It is not included in an ordinary directory listing.
Windows Server2008R2, Windows7, Windows Server2008, WindowsVista, Windows Server2003, and WindowsXP:This flag is not supported until Windows8 and Windows Server2012.
  • FILE_ATTRIBUTE_OFFLINE
    4096 (0x1000)
The data of a file is not available immediately. This attribute indicates that the file data is physically moved to offline storage. This attribute is used by Remote Storage, which is the hierarchical storage management software. Applications should not arbitrarily change this attribute.
  • FILE_ATTRIBUTE_READONLY
    1 (0x1)
A file that is read-only. Applications can read the file, but cannot write to it or delete it. This attribute is not honored on directories. For more information, see You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7.
  • FILE_ATTRIBUTE_REPARSE_POINT
    1024 (0x400)
A file or directory that has an associated reparse point, or a file that is a symbolic link.
  • FILE_ATTRIBUTE_SPARSE_FILE
    512 (0x200)
A file that is a sparse file.
  • FILE_ATTRIBUTE_SYSTEM
    4 (0x4)
A file or directory that the operating system uses a part of, or uses exclusively.系统文件
  • FILE_ATTRIBUTE_TEMPORARY
    256 (0x100)
A file that is being used for temporary storage. File systems avoid writing data back to mass storage ifsufficient cache memory is available, because typically, an application deletes a temporary file after the handleis closed. In that scenario, the system can entirely avoid writing the data. Otherwise, the data is written afterthe handle is closed.临时文件
  • FILE_ATTRIBUTE_VIRTUAL
    65536 (0x10000)
This value is reserved for system use.虚拟文件(系

出自:https://baike.baidu.com/item/WIN32_FIND_DATA

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我的理解上述宏定义:

●FILE_ATTRIBUTE_ARCHIVE——文件包含归档属性。
●FILE_ATTRIBUTE_COMPRESSED——文件和目录被压缩。
●FILE_ATTRIBUTE_DIRECTORY——找到的是一个目录。
●FILE_ATTRIBUTE_HIDDEN——文件包含隐含属性。
●FILE_ATTRIBUTE_NORMAL——文件没有其他属性。
●FILE_ATTRIBUTE_READONLY——文件包含只读属性。
●FILE_ATTRIBUTE_SYSTEM——文件包含系统属性。
●FILE_ATTRIBUTE_TEMPORARY——文件是一个临时文件
 
typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes; //文件属性
FILETIME ftCreationTime; // 文件创建时间
FILETIME ftLastAccessTime; // 文件最后一次访问时间
FILETIME ftLastWriteTime; // 文件最后一次修改时间
DWORD nFileSizeHigh; // 文件长度高32位
DWORD nFileSizeLow; // 文件长度低32位
DWORD dwReserved0; // 系统保留
DWORD dwReserved1; // 系统保留
TCHAR cFileName[ MAX_PATH ]; // 文件名
TCHAR cAlternateFileName[ 14 ]; // 格式文件名
} WIN32_FIND_DATA, *PWIN32_FIND_DATA;
 
HANDLE FindFirstFile(
    LPCTSTR lpFileName,//filename
    LPWIN32_FIND_DATA lpFindFileData//databuffer
);

参数说明

LPCTSTR lpFileName文件名(包括路径)
LPWIN32_FIND_DATA lpFindFileData 指向一个用于保存文件信息的结构体

返回值

如果调用成功返回一个句柄,可用来做为FindNextFile或 FindClose参数
调用失败 返回为INVALID_HANDLE_VALUE(即-1) ,可调用GetLastError来获取错误信息
 
BOOL FindNextFile(
HANDLE hFindFile, //searchhandle
LPWIN32_FIND_DATA lpFindFileData //databuffer
);

参数说明

HANDLE hFindFile搜索的文件句柄 函数执行的时候搜索的是此句柄的下一文件
LPWIN32_FIND_DATA lpFindFileData 指向一个用于保存文件信息的结构体
 

返回值

非零表示成功,零表示失败。如不再有与指定条件相符的文件,会将GetLastError设置成ERROR_NO_MORE_FILES
 
上述参数解析来源百度百科,然后准备工作已备好,那么以下是实例,比较简单,没有改成通用的,修改并不难若有疑问/错误望大神提出:
header files:

#include <stdio.h>
#include <string>

#define ParaPath "path"

source files:

#include "loadfileDemo.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <IO.h>

//缓存大小

#define LEN 1024

//给定路径查询该目录下所有文件,并输出文件名跟路径
bool find(char * lpPath)
{
  char findPath[LEN];  
  WIN32_FIND_DATA FindFileData; //首先了解 WIN32_FIND_DATA结构
  strcpy(findPath,lpPath);
  strcat(findPath,"*.*");  

  HANDLE hFind=::FindFirstFile(findPath,&FindFileData);// 路径,查找缓冲区为形参
  if(INVALID_HANDLE_VALUE == hFind) //查找失败 宏定义为无效值 0xFFFFFFFF 即-1
  return false;

  while(TRUE)
  {
    if(FindFileData.dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY) //判断是否是文件
  {
      if(FindFileData.cFileName[0]!='.')//排除根目录\.. \.
    {
      strcpy(findPath,lpPath);
      strcat(findPath,FindFileData.cFileName);
      printf("findPath:%s\n",findPath);
      printf("FileData:%s\n",FindFileData.cFileName);
    }
      if(!FindNextFile(hFind,&FindFileData)) //目录不为空指针往后移动
        break;
    }
  }
    FindClose(hFind);
    return true;
}

int main(void)
{
    char *str = ParaPath;

     if(find(str)) //传入需要查找的路径
      {
        printf("success!\n");
        }
     else
      {
        printf("fail!\n");
      }
      return 0;
}

注意:限定windows下使用,接口为windows提供的,若要跨平台可考虑Qt/C++

C 给定路径遍历目录下的所有文件的更多相关文章

  1. C/C++遍历目录下的所有文件(Windows/Linux篇,超详细)

    本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6815924.html. 前面的一篇文章我们讲了用Windows API遍历一个目录下的所有文 ...

  2. C/C++遍历目录下的所有文件(Windows篇,超详细)

    注: 1. 本文讨论的是怎么用Windows API遍历目录下的所有文件.除Windows API,还有一种Windows/Linux通用的方式,使用<io.h>. 2. 本文部分翻译自M ...

  3. windows 遍历目录下的所有文件 FindFirstFile FindNextFile

    Windows下遍历文件时用到的就是FindFirstFile 和FindNextFile 首先看一下定义: HANDLE FindFirstFile( LPCTSTR lpFileName, // ...

  4. php 遍历目录下的所以文件和文件夹

    <?php/** * 遍历文件夹和文件列 * @author lizhiming * @date 2016/06/30 */define('DS', DIRECTORY_SEPARATOR); ...

  5. 遍历目录下的所有文件-os.walk

    #coding:utf-8 import os for root,dirs,files in os.walk("D:"): for fileItem in files: print ...

  6. shell 遍历目录下的所有文件

    dir=/usr/local/nginx/logs for file in $dir/*; do echo $file done //结果 ./test.sh /usr/local/nginx/log ...

  7. shell编程--遍历目录下的文件

    假定目录text下有如下文件      目录:dir_1.dir_2.dir_3 文件:text_1.text_2 遍历目录下所有的文件是目录还是文件 if -- if类型: #!bin/sh for ...

  8. 复制D:\\day05目录下的所有文件到D:\\copy,并将.txt文件改为.java文件。

    **解题思路: 1.首先定义一个静态的refile方法,参数传入两个文件路径 2.要复制目录下的所有文件,首先查询File类的方法,可以使用listFiles方法得到目录下的文件 3.想到这问题基本就 ...

  9. Java遍历一个目录下的所有文件

    Java遍历一个目录下的所有文件   Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其他流类不同的是,流类关心的是文件的内容,而File类关心的是磁盘上文件的存 ...

随机推荐

  1. 【学习笔记】虚树复习记(BZOJ2286 SDOI2011 消耗战)

    想写战略游戏却想不起来虚树T^T 所以就有了这篇复习记QwQ ——简介!—— 我们在处理树上问题的时候,dfs是一个常用手段,但是我们发现,如果一棵树上只有一部分关键点,每次dfs需要访问好多不是关键 ...

  2. 如何将 不确定的有穷自动机(NFA) 转化为 确定的有穷自动机(DFA) 并将DFA最简化

    一.从NFA到DFA的转换 例如下图: DFA的每个状态都是一个由NFA中的状态构成的集合,即NFA状态集合的一个子集 r =aa*bb*cc* 二.从带有ε-边的NFA到DFA的转换 r=0*1*2 ...

  3. 51nod 1490: 多重游戏(树上博弈)

    题目链接 该题实质上是一个树上博弈的问题.要定义四种状态——2先手必胜 1先手必败 3可输可赢 0不能控制 叶子结点为先手必败态: 若某结点的所有儿子都是先手必败态,则该结点为先手必胜态: 若某结点的 ...

  4. Linux进程管理工具vmstat,iostat,pmap

    一查看内存的工具——vmstat (一)vmstat的介绍 vmstat vmstat是Virtual Memory Statistics(虚拟内存统计)的缩写 利用vmstat命令可以对操作系统的报 ...

  5. 深入理解js——非构造函数的继承

    原文来自阮一峰日志(http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.ht ...

  6. yifan的数组

    yifan的数组 时间限制: 1 Sec  内存限制: 128 MB提交: 159  解决: 47[提交][状态] 题目描述 给你一个数组,初始值都是0,然后有N个操作,每次在一段区间L,R上加W,操 ...

  7. iOS 开发加密做法

    一般做法是这样的: 客户端 每一个请求的URL中加上时间的参数.对url中的参数是排序好的. 然后对这个URL进行MD5.将这个MD5作为最后一个参数(sign)拼接到url最后. 服务端 收到请求后 ...

  8. SQL学习记录:函数(二)

    字符串函数 1.获取字符的ASCII码 语法结构: ASCII(espression)    这里的expression是一个返回char或varchar数据类型的表达式,ASCII函数仅对表达式最左 ...

  9. 高并发大流量专题---10、MySQL数据库层的优化

    高并发大流量专题---10.MySQL数据库层的优化 一.总结 一句话总结: mysql先考虑做分布式缓存,过了缓存后就做mysql数据库层面的优化 1.mysql数据库层的优化的前面一层是什么? 数 ...

  10. Windows 08 R2_组策略

    目录 目录 组策略 组策略对象GPO 实验一组策略的计算机配置 实验二组策略的用户配置 实验三首选设置 实验四组策略更改计算机桌面 常用的组策略管理模块策略 限制用户运行指定的Windows程序 隐藏 ...