C++ method to traverse files under folders
- 2020-04-02 03:10:02
- OfStack
This article illustrates the C++ method of traversing files under folders. Share with you for your reference. The details are as follows:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define LEN 1024
//Depth first recursively traverses all files in the directory
BOOL DirectoryList(LPCSTR Path)
{
WIN32_FIND_DATA FindData;
HANDLE hError;
int FileCount = 0;
char FilePathName[LEN];
//Structural path
char FullPathName[LEN];
strcpy(FilePathName, Path);
strcat(FilePathName, "\*.*");
hError = FindFirstFile(FilePathName, &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
printf(" Search failure !");
return 0;
}
while(::FindNextFile(hError, &FindData))
{
//Overthinking. And..
if (strcmp(FindData.cFileName, ".") == 0
|| strcmp(FindData.cFileName, "..") == 0 )
{
continue;
}
//Structural integrity path
wsprintf(FullPathName, "%s\%s", Path,FindData.cFileName);
FileCount++;
//Output files at this level
printf("n%d %s ", FileCount, FullPathName);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("<Dir>");
DirectoryList(FullPathName);
}
}
return 0;
}
void main()
{
DirectoryList("D:eclipse-J2EE");
}
Hope that the article described in the C++ programming to help you.