C++ How to search a folder

Hi, I'm trying to make a function for my console-based program (no GUI) that will search a specific folder (depending on user input) and then return all the files-names in that folder to a char array.

How can I solve this problem?

Any help is greatly appreciated,
thanks in advance.
boost::filesystem has interfaces to solve problems such as this.
It's preferable to store them in an std::vector<char *> or <wchar_t *>.
If you don't want to use boost, this problem is platform-dependent, for the listing of a directory content is implementet only as a system call, not in the C library (or any c++ extension).
In Linux you can call a system library function (which internally will set some cpu registers and generate an int 80h if you use an x86 arch), in Windows you have to call another system function which - in some similar way, I would guess, but Windows is not open source, so we can't know for sure - generates a kernel trap and let the file system driver do the work. So, in fact, your question should contain the information what OS you use and go into the corresponding forum.
Thanks for both replies. :)
can some one please point me to boot::filesyetem download.
In the boost home page I cannot see filesystem download.
Do I need to download the entire boost libraries?
If you're using Windows, there's an installer that lets you download only the libraries you need.
Otherwise, I think you'll have to compile the entire source.
Hi, I found out how to do it, so since you guys are generous enough to help me, I thought it would only be fair if I came back and posted my solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{	
	WIN32_FIND_DATA data;
	HANDLE h = FindFirstFile(L"c:\\directory\\*.*",&data);
	
	if( h!=INVALID_HANDLE_VALUE ) 
	{
		do
		{
			char*   nPtr = new char [lstrlen( data.cFileName ) + 1];
			for( int i = 0; i < lstrlen( data.cFileName ); i++ )
				nPtr[i] = char( data.cFileName[i] );

			nPtr[lstrlen( data.cFileName )] = '\0';
			cout << nPtr << endl;

		} while(FindNextFile(h,&data));
	} 
	else 
		cout << "Error: No such folder." << endl;
	
	FindClose(h);
	
	return 0;
}
... which is the Windows-only-in-this-version-working-system-dependent-ugly-solution. And I have to cope with such code all day long and make it portable. Boost would be the better option here... or, at least, encapsulate the system-dependent stuff in a seperate function/namespace/class/whatever.
Actually what I'm making is a program that will search any specific folder for file duplications and move all files that have the exact same file size to a different folder, where I can manually delete the duplicants. I will use it to sort out all my family pictures :)

I guess to make it portable, you'd have to use the following unix example?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sys/types.h> 
#include <dirent.h> 

using namespace std;

int main(void) 
{ 
    DIR *dir = opendir("."); 
    if(dir) 
    { 
        struct dirent *ent; 
        while((ent = readdir(dir)) != NULL) 
        { 
            cout << (ent->d_name); 
        } 
    } 
    else 
    { 
        cout << "Error opening directory" << endl; 
    } 
    return 0; 
} 
Last edited on
I guess to make it portable, you'd have to use the following unix example?

That would then be portable among POSIX-compatible systems, but still not portable to any OS implementing the libc (you couldn't use it for windows). So if you really *need* system-dependent code, encapsulate it. Otherwise, your code won't be reusable with another system (or the same system in another version). If it is encapsulated and marked properly, porting is a lot easier.
Topic archived. No new replies allowed.