Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can use findFirst,findnext,findclose to search in subdirectories?

Status
Not open for further replies.

andreask

Technical User
Sep 23, 2005
5
CY
Currently I use findfirst, findnext and findclose to search in a specified by the user directory for documents (*.doc). My problem is that I want to include in searching subfolders too. Anyone can help me? Thanks
 
Look at thread101-49144 and thread101-989298 first. Then post back here when you get stuck again.

I guess I ought to write a FAQ about this.

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
some code to peruse.

Code:
char directoryarray [5000] [256];
char filearray [5000] [100];

int RecurseDir (char *dir, char *wild, int x)
{
    // Loads the directoryarray

    struct ffblk ffblk;
    int done;
    char buff [512];

    strcpy( buff, dir );
    strcat( buff, wild );

    done = findfirst(buff, &ffblk,
            FA_DIREC + FA_SYSTEM
            + FA_HIDDEN + FA_RDONLY + FA_ARCH);

    while( !done )
    {
        if(strcmp(".", ffblk.ff_name) != 0 && strcmp("..", ffblk.ff_name) != 0)
        {
            if((ffblk.ff_attrib & FA_DIREC) == FA_DIREC)
            {
                // process directory
                strcpy(buff, dir);
                strcat(buff, ffblk.ff_name);
                strcpy (directoryarray [x], buff);
                strcat(buff, "\\");

                x++;

                x = RecurseDir(buff, wild, x);
            }
            else
            {
                //
            }
        }

        done = findnext(&ffblk);
    }

    return x;
}


int ScanDir (char *dir)
{
    // This function scans a directory for all files.
    // Loads the filearray

    struct ffblk ffblk;
    int done;
    int x = 0;
    char *buff = new char [512];

    strcpy(buff, dir);
    strcat(buff, "*.*");

    done = findfirst(buff, &ffblk, FA_DIREC + FA_SYSTEM + FA_HIDDEN + FA_RDONLY + FA_ARCH);

    while(!done)
    {
        if(strcmp(".", ffblk.ff_name) != 0 && strcmp("..", ffblk.ff_name) != 0)
        {
            if((ffblk.ff_attrib & FA_DIREC) == FA_DIREC);
            else
            {
                strcpy (filearray [x], ffblk.ff_name);

                x++;
            }
        }

        done = findnext(&ffblk);
    }

    delete buff;

    return x;
}


[\code]

I use a large array because in the app I use this code in
the entire drive directory list is sometimes loaded.  you
coud probably get by with 1/4 size or less depending upon
the usage.

TC
 
I ran across an variation of this the other day. Maybe I'll post it or its link.

Butthead, did you turn off your TGML? I notice the code /code didn't process correctly.



James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top