C++ How to search a folder

Jul 28, 2008 at 12:02am
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.
Jul 28, 2008 at 1:19am
boost::filesystem has interfaces to solve problems such as this.
It's preferable to store them in an std::vector<char *> or <wchar_t *>.
Jul 28, 2008 at 7:04am
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.
Jul 28, 2008 at 9:23am
Thanks for both replies. :)
Jul 28, 2008 at 1:40pm
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?
Jul 28, 2008 at 5:21pm
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.
Jul 29, 2008 at 11:50pm
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;
}
Jul 30, 2008 at 6:25am
... 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.
Jul 30, 2008 at 3:07pm
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 Jul 30, 2008 at 3:10pm
Jul 30, 2008 at 3:29pm
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.