Click here to Skip to main content
15,306,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Code:-
C++
static void listfiles(string boost) {

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

	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			std::cout << data.cFileName << std::endl; //prints files names in the directory
			system("puase");
		} while (FindNextFile(hFind, &data));
		FindClose(hFind);
	}
}
Posted
Updated 7-Jan-21 11:31am
v2
Comments
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?

1 solution

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

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

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

using namespace std;

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

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

	if (INVALID_HANDLE_VALUE == hFind) {
		_tprintf(TEXT("Error FindFirstFile\n"));
		return;
	}

	// 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());
			printfiles(dirstr);
		}
		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"));
	}

	FindClose(hFind);
}
   
v3
Comments
MohammedZr 8-Jan-21 0:09am
   
what about the subdirectories?
MohammedZr 8-Jan-21 8:19am
   
This is not clear!Enough any help

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