windows遍历目录下所有文件

From: http://blog.csdn.net/benbon/article/details/1911230

 

      在windows中遍历目录下的所有文件主要是使用FindFirstFile和FindNextFile通过递归调用实现的,类似于Linux的opendir和readdir。

       在MSDN中,FindFirstFile的声明如下:

HANDLE FindFirstFile(
  LPCTSTR lpFileName,
  LPWIN32_FIND_DATA lpFindFileData
);

参数说明:

lpFileName
[in] A pointer to a null-terminated string that specifies a valid directory or path, and file name that can contain wildcard characters (* and ?).

If the string ends with a wildcard, period, or directory name, the user must have access to the root and all subdirectories on the path.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "//?/" to the path. For more information, see Naming a File.

Windows Me/98/95:  This string must not exceed MAX_PATH characters.
lpFindFileData
[out] A pointer to the WIN32_FIND_DATA structure that receives information about a found file or subdirectory.

返回值说明:

If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

FindNextFile:

BOOL FindNextFile(
  HANDLE hFindFile,
  LPWIN32_FIND_DATA lpFindFileData
);

Parameters

hFindFile
[in] Search handle returned by a previous call to the FindFirstFile or FindFirstFileEx function.
lpFindFileData
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.

The structure can be used in subsequent calls to FindNextFile to see the found file or directory.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero (0). To get extended error information, call GetLastError.

If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

涉及的WIN32_FIND_DATA结构体的声明:

WIN32_FIND_DATA

 

The WIN32_FIND_DATA structure describes a file found by the FindFirstFile, FindFirstFileEx, or FindNextFile function.

 

typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[MAX_PATH];
TCHAR cAlternateFileName[14];
} WIN32_FIND_DATA, 
*PWIN32_FIND_DATA;

Members

dwFileAttributes
File attributes of the file found.

This member can be one or more of the following values.

AttributeMeaning
FILE_ATTRIBUTE_ARCHIVEThe file or directory is an archive file or directory.

Applications use this attribute to mark files for backup or removal.

FILE_ATTRIBUTE_COMPRESSEDThe file or directory is compressed.

For a file, this means that all of the data in the file is compressed.

For a directory, this means that compression is the default for newly created files and subdirectories.

FILE_ATTRIBUTE_DIRECTORYThe handle identifies a directory.
FILE_ATTRIBUTE_ENCRYPTEDThe file or directory is encrypted.

For a file, this means that all data in the file is encrypted.

For a directory, this means that encryption is the default for newly created files and subdirectories.

FILE_ATTRIBUTE_HIDDENThe file or directory is hidden.

It is not included in an ordinary directory listing.

FILE_ATTRIBUTE_NORMALThe file or directory does not have another attributes set.

This attribute is valid only if used alone.

FILE_ATTRIBUTE_OFFLINEThe file data is not immediately available.

This attribute indicates that the file data has been physically moved to offline storage.

This attribute is used by Remote Storage, the hierarchical storage management software.

Applications should not arbitrarily change this attribute.

FILE_ATTRIBUTE_READONLYThe file or directory is read-only.

Applications can read the file, but cannot write to it or delete it.

For a directory, applications cannot delete it.

FILE_ATTRIBUTE_REPARSE_POINTThe file or directory has an associated reparse point.
FILE_ATTRIBUTE_SPARSE_FILEThe file is a sparse file.
FILE_ATTRIBUTE_SYSTEMThe file or directory is part of the operating system, or is used exclusively by the operating system.
FILE_ATTRIBUTE_TEMPORARYThe file is being used for temporary storage.

File systems attempt to keep all of the data in memory for quick access, rather than flushing it back to mass storage.

A temporary file should be deleted by the application as soon as it is not needed.

ftCreationTime
A FILETIME structure that specifies when the file or directory is created.

If the underlying file system does not support creation time, this member is 0 (zero).

ftLastAccessTime
A FILETIME structure.

For a file, the structure specifies when the file is last read from, written to, or, in the case of executable files, run.

For a directory, the structure specifies when the directory is created. If the underlying file system does not support last access time, this member is 0 (zero).

On the FAT file system, the specified date for both files and directories is correct, but the time of day is always set to midnight.

ftLastWriteTime
A FILETIME structure.

For a file, the structure specifies when the file is last written to, truncated, or overwritten (for example, with WriteFile or SetEndOfFile). This date and time is not updated when file attributes or security descriptors are changed.

For a directory, the structure specifies when the directory is created. If the underlying file system does not support last write time, this member is 0 (zero).
nFileSizeHigh
High-order DWORD value of the file size, in bytes.

This value is 0 (zero) unless the file size is greater than MAXDWORD.

The size of the file is equal to (nFileSizeHigh * (MAXDWORD+1)) + nFileSizeLow.

nFileSizeLow
Low-order DWORD value of the file size, in bytes.
dwReserved0
If the dwFileAttributes member includes the FILE_ATTRIBUTE_REPARSE_POINT attribute, this member specifies the reparse tag.

Otherwise, this value is undefined and should not be used.

dwReserved1
Reserved for future use.
cFileName
A null-terminated string that specifies the name of a file.
cAlternateFileName
A null-terminated string that specifies an alternative name for the file.

This name is in the classic 8.3 (filename.ext) file name format.

下面是实现的一个例子:

 

/**//*******************************
 *     函数名:FindFile
 *     输入参数:pFilePath(路径)
 *     输出参数:无
 *     功能
********************************
*/

void FindFile(char * pFilePath)
...{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind 
= INVALID_HANDLE_VALUE;
    
char DirSpec[MAX_PATH + 1];// 指定路径
    DWORD dwError;

    strncpy (DirSpec, pFilePath, strlen(pFilePath) 
+ 1);
    strncat (DirSpec, 
"/*"3);

    hFind 
= FindFirstFile((DirSpec, &FindFileData);

    
if (hFind == INVALID_HANDLE_VALUE)
    
...{
        printf (
"Invalid file handle. Error is %u ", GetLastError());
        
return ;
    }

    
else
    
...{
        
if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
        
...{
            printf (
"  %s ", FindFileData.cFileName);   //找到文件
        }

        
else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
            
&& strcmp(FindFileData.cFileName, "."!= 0
            
&& strcmp(FindFileData.cFileName, ".."!= 0)
        
...{   //找到目录
            
char Dir[MAX_PATH + 1];
            strcpy(Dir, pFilePath);
            strncat(Dir, 
"/"2);
            strcat(Dir, FindFileData.cFileName);

            FindFile(Dir);
        }


        
while (FindNextFile(hFind, &FindFileData) != 0)
        
...{
            
if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
            
...{   //找到文件
                printf (
"  %s ", FindFileData.cFileName);
            }

            
else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
                
&& strcmp(FindFileData.cFileName, "."!= 0
                
&& strcmp(FindFileData.cFileName, ".."!= 0)
            
...{ //找到目录
                
char Dir[MAX_PATH + 1];
                strcpy(Dir, pFilePath);
                strncat(Dir, 
"/"2);
                strcat(Dir, FindFileData.cFileName);
                FindFile(Dir);
            }


        }


        dwError 
= GetLastError();
        FindClose(hFind);
        
if (dwError != ERROR_NO_MORE_FILES)
        
...{
            printf (
"FindNextFile error. Error is %u ", dwError);
            
return;
        }

    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407509.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue之计算属性Computed

计算属性将被添加到Vue的实例中。计算属性内部的getter和setter函数内的this上下文将自动地绑定为Vue实例 不应该使用箭头函数来定义计算属性函数 (例如 aDouble: () > this.a * 2)。理由是箭头函数绑定了父级作用域的上下文,所以 this 将不会按照期望指向 Vue …

虚拟机几种网络连接方式的区别

虚拟机,不论是VirtualBox还是VMWare,都提供了几种网络连接方式,其中包括了桥接(Bridged)、网络地址翻译(NAT)和主机模式(Host-only)。 桥接(Bridged)相当于在主机所在的局域网中增加了一台机器,主机和虚拟机的IP地址都是主机所在的…

使用AIR运行另外的程序。

NavigateToURL方法.<?xml version"1.0" encoding"utf-8"?><mx:WindowedApplication xmlns:mx"http://www.adobe.com/2006/mxml"layout"absolute"><mx:Script><![CDATA[import flash.net.URLRequest; pu…

ftw遍历目录树

表头文件&#xff1a;#include <ftw.h>定义函数&#xff1a;int ftw(const char *dir, int (*fn) (const *file, const struct stat *sb, int flag), int depth)函数说明&#xff1a;ftw() 会从参数dir指定的 目录开始&#xff0c;往下一层层地递归式遍历子 目录。ftw()…

关于用display:table让元素居中的小结

让元素垂直居中有一种简单的方法:给需要居中的元素用一个父级包起来&#xff0c;并给父元素添加样式’display&#xff1a;table’,同时给这个父级设置好高度,再给需要居中的元素一个display&#xff1a;table-cell,vertical-align:middle;这样被设置的元素就可以做到垂直居中 …

谷歌联合 Adobe 发布 Noto 字体【免费下载】

Noto 涵盖了世界上所有主要语言&#xff0c;包括欧洲&#xff0c;非洲&#xff0c;中东&#xff0c;印度语&#xff0c;南亚和东南亚&#xff0c;中亚&#xff0c;美洲和东亚语言。也支持几个少数民族和历史语言。不久前&#xff0c;还发布了针对文、日文、韩文的开源字体——N…

5月8日全国软考办专家做客51CTO谈:软考政策、考前复习方法及考场技巧

访谈实录>>软考专家访谈实录之一&#xff1a;分数线和通过率是如何确定的&#xff1f;软考专家访谈实录之二&#xff1a;今年的试题会增加难度吗&#xff1f;软考专家访谈实录之三&#xff1a;论文应考秘笈2008年上半年全国软考时间&#xff1a;5月24日、25日。软考临近&…

Vue之引用DOM的ref属性

ref 被用来给DOM元素或子组件注册引用信息。引用信息会根据父组件的 $refs 对象进行注册。如果在普通的DOM元素上使用&#xff0c;引用信息就是元素; 如果用在子组件上&#xff0c;引用信息就是组件实例 注意&#xff1a;只要想要在Vue中直接操作DOM元素&#xff0c;就必须用r…

Neutron中的Service类

Service是OpenStack中非常重要的一个概念&#xff0c;各个服务的组件都以Service类的方式来进行交互。 Neutron中的Service类继承自rpc中的Service&#xff0c;总体的继承关系为 neutron.openstack.common.service.Service类-->neutron.common.rpc.Service类-->neutron.s…

[Linux C]列出指定目录下的所有文件(夹)

在Linux下&#xff0c;用C语言实现列出指定目录下的所有文件和文件夹&#xff1a; #include <stdio.h>#include <dirent.h>#include <stdlib.h>int main(){DIR *dirp;struct dirent *direntp;dirp opendir("/home/zcm/program/eclipse");if(dirp…

打扫房间,做减法

今天和很多同事都聊了聊天&#xff0c;对于现有的产品&#xff0c;如何更好的修订&#xff0c;大致形成了一个思路&#xff1a; 打扫房间&#xff0c;让产品更加的干净&#xff0c;使其易于维护和修订 做减法&#xff0c;去掉无用和临时增加的功能&#xff0c;使其更加易用 定期…

NetBeans中文乱码解决办法

首先来看看网上的一篇文章&#xff1a;http://it.dengchao.org/neatbeans-problem-fedora/linux/ 在Windows和Linux(Fedora/Ubuntu/RedHat)中安装了NetBeans后&#xff0c;会遇到菜单等显示乱码的问题。这里告诉大家如何解决中文显示乱码的问题&#xff0c;包括Windows、Fedor…

bashrc,bash_profile和/etc/profile

bashrc,bash_profile和/etc/profile 最近老出现在shell里面能跑的程序用鼠标双击app去不能跑.究其原因是因为环境变量的问题. 在类unix系统中一般有三个bash配置文件: ~/.bashrc 当前用户使用的配置文件~/.bash_profile 当前用户使用的配置文件/etc/profile 所有用户都会继承的…

Vue之$nextTick属性

因为Vue中DOM的更新是异步的&#xff0c;所以一般涉及到DOM的操作都会放在nextTick函数的回调中去执行 将回调延迟到下次 DOM 更新循环之后再执行。在修改数据之后立即使用它&#xff0c;然后等待 DOM 更新之后执行回调。它跟全局方法 Vue.nextTick 一样&#xff0c;不同的是回…

小程序input实现数据双向绑定

小程序input实现数据双向绑定最终效果index.wxmlindex.js最终效果 index.wxml <view class"uploader"><input type"number" style"border: 2rpx solid #666;" bindinputinputChange data-namepeopleNumber placeholder"输入人数&…