Sign in to follow this  
Malchivus

how to list files in a folder?

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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this