C 给定路径遍历目录下的所有文件
在此之前需要了解 WIN32_FIND_DATA的结构 以及 FindFirstFile、 FindNextFile原型以及用法注意事项传送门如下
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365740(v=vs.85).aspx
涉及的宏定义
------------------------------------------------------------------------------------------------------------------------------------------------
|
A file or directory that is an archive file or directory. Applications typically use this attribute to markfiles for backup or removal.存档类
|
|
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.
|
|
This value is reserved for system use.驱动类
|
|
The handle that identifies a directory.目录类
|
|
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.
|
|
The file or directory is hidden. It is not included in an ordinary directory listing.隐藏
|
|
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.
|
|
A file that does not have other attributes set. This attribute is valid only when used alone.普通
|
|
The file or directory is not to be indexed by the content indexing service.
|
|
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.
|
|
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.
|
|
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.
|
|
A file or directory that has an associated reparse point, or a file that is a symbolic link.
|
|
A file that is a sparse file.
|
|
A file or directory that the operating system uses a part of, or uses exclusively.系统文件
|
|
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.临时文件
|
|
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——文件是一个临时文件HANDLE FindFirstFile( LPCTSTR lpFileName,//filename LPWIN32_FIND_DATA lpFindFileData//databuffer);参数说明
返回值
参数说明
返回值
#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;
}
C 给定路径遍历目录下的所有文件的更多相关文章
- C/C++遍历目录下的所有文件(Windows/Linux篇,超详细)
本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6815924.html. 前面的一篇文章我们讲了用Windows API遍历一个目录下的所有文 ...
- C/C++遍历目录下的所有文件(Windows篇,超详细)
注: 1. 本文讨论的是怎么用Windows API遍历目录下的所有文件.除Windows API,还有一种Windows/Linux通用的方式,使用<io.h>. 2. 本文部分翻译自M ...
- windows 遍历目录下的所有文件 FindFirstFile FindNextFile
Windows下遍历文件时用到的就是FindFirstFile 和FindNextFile 首先看一下定义: HANDLE FindFirstFile( LPCTSTR lpFileName, // ...
- php 遍历目录下的所以文件和文件夹
<?php/** * 遍历文件夹和文件列 * @author lizhiming * @date 2016/06/30 */define('DS', DIRECTORY_SEPARATOR); ...
- 遍历目录下的所有文件-os.walk
#coding:utf-8 import os for root,dirs,files in os.walk("D:"): for fileItem in files: print ...
- shell 遍历目录下的所有文件
dir=/usr/local/nginx/logs for file in $dir/*; do echo $file done //结果 ./test.sh /usr/local/nginx/log ...
- shell编程--遍历目录下的文件
假定目录text下有如下文件 目录:dir_1.dir_2.dir_3 文件:text_1.text_2 遍历目录下所有的文件是目录还是文件 if -- if类型: #!bin/sh for ...
- 复制D:\\day05目录下的所有文件到D:\\copy,并将.txt文件改为.java文件。
**解题思路: 1.首先定义一个静态的refile方法,参数传入两个文件路径 2.要复制目录下的所有文件,首先查询File类的方法,可以使用listFiles方法得到目录下的文件 3.想到这问题基本就 ...
- Java遍历一个目录下的所有文件
Java遍历一个目录下的所有文件 Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其他流类不同的是,流类关心的是文件的内容,而File类关心的是磁盘上文件的存 ...
随机推荐
- CF240E Road Repairs
最小树形图+输出方案 输出方案的话记录一下哪些边 然后记得最后拆环要倒着拆就行了
- centos 6.5 配置阿里云 yum 镜像
配置国内镜像目的是为了加速软件下载安装速度,参考链接:http://mirrors.aliyun.com/help/centos 备份.养成文件操作前备份的习惯 cd /etc/yum.repos.d ...
- Ubuntu查看和自动挂载硬盘
sudo blkid 查看UUID vim /etc/fstab 进行修改 如果 fstab 文件中的命令挂载的硬盘不存在,启动的时候会报错.
- Raspbian 在虚拟机上运行,运行Flask,供宿主机访问
Raspbian 在虚拟机上运行,启动Flask,供宿主机访问 参考ref 1, 在virtualbox上跑起来Raspbian OS 参考ref 2, 在Raspbian上安装并运行Falsk, 注 ...
- mui is not defined
vue项目中引用mui.js,我是在main.js中这样引入的, 结果报错 查找资料,最后在mui.js的最后添加了这样一句 这是因为mui并不能像jquery那样作为全局对象存在,加上wi ...
- Git 中的一些其他常用命令
1.查看提交的历史版本(git log) 我们可以使用 git log 命令来查看提交的历史版本. 默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面.每个版本都有 ...
- Gradle Wrapper 介绍
回顾 上一篇介绍了如何使用 Gradle 快速创建 Java 项目,开始讲 Gradle Wrapper 之前先来重温一下通过 gradle init 命令创建的 Java 项目目录结构 ├── bu ...
- zabbix真的很简单 (安装篇)
系统环境: Centos 6.4 一直觉得 zabbix 很简单,但是还是有好多人看了好多文档都搞不明白怎么用,我从2013年使用到现在也小有心得,如果时间允许,很高兴与大家一起分享我在使用过程中的一 ...
- linux下的命令是如何运行的
linux下的命令分为内建命令.可执行文件.脚本文件 shell终端里键入一个命令,如ls.cd.bash,shell会先查询一个环境变量PATH,它存了各种可执行文件的路径,输入$PATH可以打印变 ...
- VB - 变量
Cbool函数将变量转换成布尔值: Cbyte函数将变量转换为0到255之间的整数. Ccur函数.Cdbl函数和Csng函数将变量转换为浮点数值,前者只精确到小数点后四位,后两者要更加精确,数值的范 ...