Win32 File API FindNextFile function

Download EaseFilter Monitor, Control and Encryption Filter Driver SDK Setup File
Download EaseFilter Monitor, Control and Encryption Filter Driver SDK Zip File

Continues a file search from a previous call to the FindFirstFileFindFirstFileEx, orFindFirstFileTransacted functions.


BOOL FindNextFile(    
        HANDLE             hFindFile,    
        LPWIN32_FIND_DATAW lpFindFileData  );  



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


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 returnsERROR_NO_MORE_FILES.


This function uses the same search filters that were used to create the search handle passed in thehFindFile parameter. For additional information, see FindFirstFile and FindFirstFileEx.

The order in which the search returns the files, such as alphabetical order, is not guaranteed, and is dependent on the file system. If the data must be sorted, the application must do the ordering after obtaining all the results.

Note  In rare cases or on a heavily loaded system, file attribute information on NTFS file systems may not be current at the time this function is called. To be assured of getting the current NTFS file system file attributes, call the GetFileInformationByHandle function.
The order in which this function returns the file names is dependent on the file system type. With the NTFS file system and CDFS file systems, the names are usually returned in alphabetical order. With FAT file systems, the names are usually returned in the order the files were written to the disk, which may or may not be in alphabetical order. However, as stated previously, these behaviors are not guaranteed.

If the path points to a symbolic link, the WIN32_FIND_DATA buffer contains information about the symbolic link, not the target.


The following example calls FindFirstFileFindNextFile, and FindClose to list files in a specified directory.

#include <windows.h>  
#include <tchar.h>   
#include <stdio.h>  
#include <strsafe.h>  
#pragma comment(lib, "User32.lib")    

void DisplayErrorBox(LPTSTR lpszFunction);   

int _tmain(int argc, TCHAR *argv[])  
 	WIN32_FIND_DATA ffd;     
	LARGE_INTEGER filesize;     
	TCHAR szDir[MAX_PATH];     
	size_t length_of_arg;     
	DWORD dwError=0;          

	// If the directory is not specified as a command-line argument,     
	// print usage.      
	 if(argc != 2)     
     {    _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);        
	     return (-1);     
     // Check that the input path plus 3 is not longer than MAX_PATH.     
	 // Three characters are for the "\*" plus NULL appended below.    
     StringCchLength(argv[1], MAX_PATH, &length_of_arg);
     if (length_of_arg > (MAX_PATH - 3))     
     _tprintf(TEXT("\nDirectory path is too long.\n"));        
     return (-1);     
    _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);       
    // Prepare string for use with FindFile functions.  First, copy the     
    // string to a buffer, then append '\*' to the directory name.    
    StringCchCopy(szDir, MAX_PATH, argv[1]);     
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));       
    // Find the first file in the directory.       
    hFind = FindFirstFile(szDir, &ffd);       
    if (INVALID_HANDLE_VALUE == hFind)      
        return dwError;     
     // List all the files in the directory with some info about them.       
     	if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)        {           
        	_tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);        }        
        	filesize.LowPart = ffd.nFileSizeLow;
            filesize.HighPart = ffd.nFileSizeHigh;
            _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);        
     }while (FindNextFile(hFind, &ffd) != 0);        
     dwError = GetLastError();     
     if (dwError != ERROR_NO_MORE_FILES)      
     return dwError;