FindFirstFile

 

Contents

Description

Searches a directory for a file whose name matches the specified file name (wildcards are allowed). One can use this function in combination with FindNextFile and FindClose to find all the files in the specified directory.

Syntax

HANDLE FindFirstFile( LPCSTR lpFileName, WIN32_FIND_DATAA * lpFindFileData )

Parameters

lpFileName
[input] the wildcard pattern which the file name must match
lpFindFileData
[input] pointer to a structure WIN32_FIND_DATAA which will receive the information
about each file

Return

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.

Examples

EX1

int FindFirstFile_ex1()
{
    // case1: List all file under c:
    string strPath = "c:\\*.*";
    FindFirstFile_dir(strPath);
    return 1;
}
// a DOS dir command
// can type into ScriptWindow "dir c:\*.*", for example.
//
void FindFirstFile_dir(string strFile)
{
    WIN32_FIND_DATAA    find;
    HANDLE    hFile;

    //out_str(strFile);
    int    nCount = 0;
    
    if((hFile=FindFirstFile(strFile,&find)) != INVALID_HANDLE_VALUE)
    {
        out_str(find.cFileName);
        nCount++;
        while(FindNextFile(hFile,&find))
        {
            out_str(find.cFileName);
            nCount++;
        }
        printf("%d files found\n",nCount);
        FindClose(hFile);        // must close  the handle
    }
    else
        printf("%s: file(s) not found\n",strFile);
}

Remark

See Also

FindNextFile, FindClose

header to Include

origin.h

Reference