Public Group

# how to list files in a folder?

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 ...

##### Share on other sites
I would use boost::filesystem.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by SiCraneI 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 guyatonThere 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.

##### Share on other sites
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.

##### Share on other sites
sicrane..you worship boost don't you?..lol

##### Share on other sites
Quote:
Original post by Malchivus
Quote:
 Original post by SiCraneI 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 Postersicrane..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.

##### Share on other sites
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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015433
×