Sign in to follow this  
Kranar

Iterating through files in a directory.

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

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