Windows API 之 FineFirstFile、FindNextFile

参考:https://msdn.microsoft.com/en-us/library/aa364418%28VS.85%29.aspx

FindFirstFile

Searches a directory for a file or subdirectory with a name that matches a specific name (or partial name if wildcards are used).

BOOL WINAPI FindNextFile(
  _In_  HANDLE            hFindFile,
  _Out_ LPWIN32_FIND_DATA lpFindFileData
);

Parameters

lpFileName [in]

The directory or path, and the file name, which can include wildcard characters, for example, an asterisk (*) or a question mark (?).

This parameter should not be NULL, an invalid string (for example, an empty string or a string that is missing the terminating null character), or end in a trailing backslash (\).

If the string ends with a wildcard, period (.), or directory name, the user must have access permissions 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.

lpFindFileData [out]

A pointer to the WIN32_FIND_DATA structure that receives information about a found file or directory.

参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa364428%28v=vs.85%29.aspx

FindNextFile function

Continues a file search from a previous call to the FindFirstFile, FindFirstFileEx, or FindFirstFileTransacted functions.

BOOL WINAPI FindNextFile(
  _In_  HANDLE            hFindFile,
  _Out_ LPWIN32_FIND_DATA lpFindFileData
);

Parameters

hFindFile [in]

The search handle returned by a previous call to the FindFirstFile or FindFirstFileEx function.

lpFindFileData [out]

A pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.

Return value

If the function succeeds, the return value is nonzero and the lpFindFileData parameter contains information about the next file or directory found.

If the function fails, the return value is zero and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.

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

参考:

http://www.oschina.net/code/piece_full?code=6095&piece=9712#9712

遍历目录:

int SearchStrHead(string szCur2)
{
    int i = 0;
    WIN32_FIND_DATA FindData = { 0 };
    HANDLE hTravseDir = FindFirstFile(szCur2.c_str(), &FindData);
    //cout <<"First:" << FindData.cFileName << endl;

    if (hTravseDir == INVALID_HANDLE_VALUE)
    {

        cout << GetLastError() << endl;
        return 0;
    }

    while (FindNextFile(hTravseDir, &FindData))
    {
        if (strcmp(FindData.cFileName, ".") == 0
            || strcmp(FindData.cFileName, "..") == 0)
        {
            continue;
        }
        //cout << i++ << " ";
        //cout << FindData.cFileName << endl;
        if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            string szCur3 = szCur2;
            szCur3.erase(szCur3.find_last_of("*"));
            //string sTemp = ‘\\‘ + (string)FindData.cFileName;
            szCur3 += "\ ";
            szCur3 += FindData.cFileName;
            szCur3 += "\\*";
            szCur3.erase(szCur3.find_last_of(" "), 1);
            cout << szCur3 << endl;

            SearchStrHead(szCur3);
        }
    }
    FindClose(hTravseDir);
    return 0;
}
时间: 2024-08-15 09:35:29

Windows API 之 FineFirstFile、FindNextFile的相关文章

Windows API 大全

常用Windows API1. API之网络函数WNetAddConnection 创建同一个网络资源的永久性连接WNetAddConnection2 创建同一个网络资源的连接WNetAddConnection3 创建同一个网络资源的连接WNetCancelConnection 结束一个网络连接WNetCancelConnection2 结束一个网络连接WNetCloseEnum 结束一次枚举操作WNetConnectionDialog 启动一个标准对话框,以便建立同网络资源的连接WNetDis

一些Windows API导致的Crash以及使用问题总结(API的AV失败,可以用try catch捕捉后处理)

RegQueryValueEx gethostbyname/getaddrinfo _localtime64 FindFirstFile/FindNextFile VerQueryValue CreateFileMapping相关 SetDllDirectory Windows API就没有问题.没有BUG吗?答案是否定的!代码都是写出来,怎么可能完全没有问题呢?下面我们就来看看目前发现有哪些Windows API是有问题的,或者说使用上面有误区的. 1.RegQueryValueEx 首先看看

初识【Windows API】

最近学习操作系统中,老师布置了一个作业,运用系统调用函数删除文件夹下两个重复文本类文件,Linux玩不动,于是就只能在Windows下进行了. 看了一下介绍Windows API的博客: 点击打开 基本就开始动手了. 主要利用的函数其实就那么几个: CreateFile      创建.打开文件ReadFile        读取文件内容DeleteFile      文件删除FindFirstFile   查找指定目录下的第一个文件FindNextFile   查找下一个文件GetFileAt

Windows API教程文件系统

本篇文章主要介绍了"Windows API教程文件系统",主要涉及到Windows API教程文件系统方面的内容,对于Windows API教程文件系统感兴趣的同学可以参考一下. 索引 概念简介 文件对象 文件流 文件句柄 文件指针 文件系统操作 常见 API 高级文件操作 本讲程序功能列表 CreateFile 具体参数 返回值 DeleteFile 参数 返回值 CopyFile.MoveFile.FindFirstFile ReadFile GetCurrentDirectory

WINDOWS API函数说明

Windows API函数大全,从事软件开发的朋友可以参考下 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同一个网络资源的连接 WNetCancelConnection 结束一个网络连接 WNetCancelConnection2 结束一个网络连接 WNetCloseEnum 结束一次枚举操作 WNetConnectionDialog 启动一个

Windows API 编程学习记录&lt;二&gt;

恩,开始写Windows API编程第二节吧. 上次介绍了几个关于Windows API编程最基本的概念,但是如果只是看这些概念,估计还是对Windows API不是很了解.这节我们就使用Windows API 让大家来了解下Windows API的用法. 第一个介绍的Windows API 当然是最经典的MessageBox,这个API 的作用就是在电脑上显示一个对话框,我们先来看看这个API的定义吧: int WINAPI MessageBox(HWND hWnd, LPCTSTR lpTe

Windows API 编程学习记录&lt;三&gt;

恩,开始写API编程的第三节,其实马上要考试了,但是不把这节写完,心里总感觉不舒服啊.写完赶紧去复习啊       在前两节中,我们介绍了Windows API 编程的一些基本概念和一个最基本API函数 MessageBox的使用,在这节中,我们就来正式编写一个Windows的窗口程序. 在具体编写代码之前,我们必须先要了解一下API 编写窗口程序具体的三个基本步骤:             1. 注册窗口类:             2.创建窗口:             3.显示窗口: 恩,

WinSpy涉及的windows api

WinSpy涉及的windows api WinSpy是仿造微软Spy++的开源项目,但只涉及Spy++的窗口句柄.窗口的属性.styles.类名子窗口.进程线程信息等查找功能.功能虽然不算强大,但涉及到很多windows api,是了解windows api的一个有用工具.WinSpy界面截图如下: 1:拖拽瞄准镜图标获取窗口的HWND 核心api:ClientToScreen.WindowFromPoint.EnumChildWindows.GetParent.GetWindowLong.S

Delphi Windows API判断文件共享锁定状态

一.概述 锁是操作系统为实现数据共享而提供的一种安全机制,它使得不同的应用程序,不同的计算机之间可以安全有效地共享和交换数据.要保证安全有效地操作共享数据,必须在相应的操作前判断锁的类型,然后才能确定数据是否可读或可写,从而为开发出健壮的程序提供切实依据.   同样,在Windows中,文件可以共享模式打开,它也涉及到锁的操作问题.根据Windows中文件共享时加锁范围的大小,锁可分为全局锁和局部锁:全局锁以锁定文件全部内容为特征,而局部锁以锁定文件的局部内容为特征,且文件的锁定区域不可重复.根