Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I made a code that saves files name as a string for all files that exist in the same directory and iterate to print every file, but I want the function to print the file name in the subdirectories also.

What I have tried:

static void listfiles(string boost) {

	WIN32_FIND_DATA data;
	HANDLE hFind = FindFirstFile("C:/Users/zurghni/source/repos/Project13/Debug/grko/gslp/*", &data);      // DIRECTORY

		do {
			std::cout << data.cFileName << std::endl; //prints files names in the directory
		} while (FindNextFile(hFind, &data));
Updated 17hrs ago
enhzflep 7-Jan-21 16:54pm
Okay, fine. So what do you want to know?
You ahhh, you forgot to ask a question....
MohammedZr 7-Jan-21 16:56pm
I want to know how can I get the names of files in the subdirectories of a folder
for example, I have another folder inside the ``gslp`` folder that contains a bunch of files how can I get their names ?? and print them
enhzflep 7-Jan-21 17:02pm
Same way you got the names for this folder. I suspect, you're trying to tell which of the entries in the current folder are directories, and from there, list their contents as well.
Am I correct in thinking that the current barrier is determining which of the entries in the current directory are directories themselves?

Listing the Files in a Directory - Win32 apps | Microsoft Docs[^]

You have to check the Attribute and append new directory name in
to path and call FindFirst again.

// A TCHAR based std::string
typedef std::basic_string<TCHAR> tstring;

using namespace std;

void printfiles(tstring inputstr)
	tstring seachstr = inputstr + TEXT("\\*");
	wcscpy_s(szDir, MAX_PATH, seachstr.c_str());

	// Find the first file in the directory.
	HANDLE hFind = FindFirstFile(szDir, &ffd);

		_tprintf(TEXT("Error FindFirstFile\n"));

	// List all the files in the directory with some info about them
	do {
		if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			if (!_tcscmp(ffd.cFileName, TEXT(".")) || 
			   !_tcscmp(ffd.cFileName, TEXT(".."))) continue;
			tstring dirstr = inputstr + TEXT("\\") + ffd.cFileName;
			_tprintf(TEXT("  %s   <DIR>\n"), dirstr.c_str());
		else {
			LARGE_INTEGER filesize;
			filesize.LowPart = ffd.nFileSizeLow;
			filesize.HighPart = ffd.nFileSizeHigh;
			_tprintf(TEXT("  %s   %lld bytes\n"), 
			   ffd.cFileName, filesize.QuadPart);
	} while (FindNextFile(hFind, &ffd) != 0);

	DWORD dwError;
	dwError = GetLastError();
	if (dwError != ERROR_NO_MORE_FILES) {
		_tprintf(TEXT("Error while FindFile\n"));

MohammedZr 8-Jan-21 0:09am
what about the subdirectories?
MohammedZr 8-Jan-21 8:19am
This is not clear!Enough any help
If you can use C++17, you can do this in a platform-independent way using filesystem[^]. The function ListFiles in this .cpp[^] should give you an idea how to use it. It creates a list of the paths to all files in a specified directory but avoids subdirectories. You can change the logic to call the function recursively if you want to include files in subdirectories.
Richard Deeming 3hrs 15mins ago
The OP hasn't posted anything since last August, so don't hold your breath waiting for a reply. :)
Greg Utas 1hr 15mins ago
I hope somebody finds it useful. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900