List Files Folders in a Directory in C++ Windows

Below is an example of how to loop over all files and folders in a given location and output the names.

#include <Windows.h>
int main(int argc, char** argv)
{
	HANDLE hFind = INVALID_HANDLE_VALUE;
	WIN32_FIND_DATA ffd;

	hFind = FindFirstFile(TEXT("C:\\temp\\*"), &ffd);
	do
	{
		Sleep(1000);
		bool isDirectory = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
		if(isDirectory)
		{
			cout << "DirectoryName: " << ffd.cFileName << endl;
		}
		else
		{
			cout << "FileName: " << ffd.cFileName << endl;
		}
	}while(FindNextFile(hFind, &ffd) != 0);
	FindClose(hFind);
}

References:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx

Was my post helpful? :)
Leave a Comment

Notify via Email Only if someone replies to My Comment