Jump to content
  • Advertisement
Sign in to follow this  
Kranar

Iterating through files in a directory.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I go about doing this in Windows, using CFile? I am trying to make a program that goes through a whole bunch of logs to keep track of some simple information, but the filenames of these text logs are arbitrary. Is there a way to open the first file in the directory, scan through it, close it, then go to the next file and repeat until all files have been read? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Hello,

There is a way to do this using MFC. Check the documentation for the CFileFind MFC class - it will allow you to read a directory content and to iterate through the files.

HTH

Share this post


Link to post
Share on other sites
i would suggest the portable 'good-ol-C' way of doing this

pretty self explanitory.

And if you still want the MFC way thats not too hard either.


Personaly, i _always_ use this method, even in my MFC apps.

/* FFIND.C: This program uses the 32-bit _find functions to print
* a list of all files (and their attributes) with a .C extension
* in the current directory.
*/


#include <stdio.h>
#include <io.h>
#include <time.h>

void main( void )
{
struct _finddata_t c_file;
long hFile;

/* Find first .c file in current directory */
if( (hFile = _findfirst( "*.c", &c_file )) == -1L )
printf( "No *.c files in current directory!\n" );
else
{
printf( "Listing of .c files\n\n" );
printf( "\nRDO HID SYS ARC FILE DATE %25c SIZE\n", ' ' );
printf( "--- --- --- --- ---- ---- %25c ----\n", ' ' );
printf( ( c_file.attrib & _A_RDONLY ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_ARCH ) ? " Y " : " N " );
printf( " %-12s %.24s %9ld\n",
c_file.name, ctime( &( c_file.time_write ) ), c_file.size );

/* Find the rest of the .c files */
while( _findnext( hFile, &c_file ) == 0 )
{
printf( ( c_file.attrib & _A_RDONLY ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_ARCH ) ? " Y " : " N " );
printf( " %-12s %.24s %9ld\n",
c_file.name, ctime( &( c_file.time_write ) ), c_file.size );
}

_findclose( hFile );
}
}





MFC example:

CFileFind finder;
BOOL bWorking = finder.FindFile("*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
cout << (LPCTSTR) finder.GetFileName() << endl;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!