# Iterating through files in a directory.

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

## 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 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 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;   }

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632579
• Total Posts
3007178

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!