how to list files in a folder?

Started by
6 comments, last by Malchivus 18 years, 7 months ago
Heya, I know this is something that you see on the internet, but I can't seem to get anything to do it... I am using mingw32, is there a (GCC) way (i.e. a set of functions in one of the libs) that will let me find out all of the files in a directory? I know there are things for windows, and for *nix, but I want to do it in a way that works everywhere ... or is this one of those places where you need to write a separate path for each platform ...
I just wanna get this done.
Advertisement
I would use boost::filesystem.
There is a windows specific way if you want.
it looks like this:

WIN32_FIND_DATA FindFileData;HANDLE hFind;printf ("Target file is %s.\n", argv[1]);hFind = FindFirstFile("*\0", &FindFileData);if (hFind == INVALID_HANDLE_VALUE) {	printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ());	return (0);} do{	printf ("The first file found is %s\n", FindFileData.cFileName);	if( FindFileData.dwFileAttributes  == FILE_ATTRIBUTE_DIRECTORY )	{		if( strcmp( FindFileData.cFileName, ".\0" ) && strcmp( FindFileData.cFileName, "..\0" ) )		{			printf( "\n" );			//should recurse here			WIN32_FIND_DATA FindFileData1;			HANDLE hFind1;			string st;			st.append( FindFileData.cFileName );			st.append( "\\*" );			printf( "string=%s\n", st.c_str() );						hFind1 = FindFirstFile( st.c_str(), &FindFileData1 );							cout<<  "\n%s\n"<< FindFileData1.cFileName <<endl;			cout<< "%S\n" << FindFileData1.cAlternateFileName <<endl;		} 	}}while( FindNextFile( hFind, &FindFileData ) );FindClose(hFind);


havn't compiled it so i can't promise anything but give it a shot (it will recurse through sub directories t0o.

~guyaton
~guyaton
Quote:Original post by SiCrane
I would use boost::filesystem.


hmm, looks a bit heavy for what i want to do, also seems that the library will need to be rebuilt for every platform ... thats what i was trying to avoid ...

Quote:Original post by guyaton
There is a windows specific way if you want.
it looks like this:

*** Source Snippet Removed ***

havn't compiled it so i can't promise anything but give it a shot (it will recurse through sub directories t0o.

~guyaton


thanks, that will work for windows ...

i guess that this just isn't something that can be done without relying on a specific system API, or including more libraries in my application ... thanks anyway.
I just wanna get this done.
Quote:Original post by Malchivus
seems that the library will need to be rebuilt for every platform ... thats what i was trying to avoid ...

I fail to see why that would be a problem since you need to rebuild your application for every platform. Also you don't need to build boost as a library. It's perfectly feasible to add the four or five source files that go into making up boost::filesystem as source files in your application.
sicrane..you worship boost don't you?..lol
Quote:Original post by Malchivus
Quote:Original post by SiCrane
I would use boost::filesystem.


hmm, looks a bit heavy for what i want to do, also seems that the library will need to be rebuilt for every platform ... thats what i was trying to avoid ...


Given that there is no standard API that will work on every system you will have to rely on a third party library. If you're not interested in using Boost, you can use the Apache Portable Runtime instead.

Having to rebuild the library is really a minor issue when compared to the need to rewrite and rebuild your code.

Quote:Original post by Anonymous Poster
sicrane..you worship boost don't you?..lol


It works, provides a lot of widely needed facilities... why not use it? I see nothing wrong with that.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
i didn't understand that there was no prtable way to do it. i thought that somewhere burried in gcc(mingw) there would be something. oops
I just wanna get this done.

This topic is closed to new replies.

Advertisement