Results 1 to 3 of 3

Thread: Recursive directory search

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    Thumbs down Recursive directory search

    Subject: Recursive directory listing using FindFirstFile,FindNextFile etc
    O.S: w2000 pro
    Compiler VC++ 6
    App: console

    Hello

    Trying to write a recursive function (my first) to search directories for files that meet a criteria. I’m nearly there ,but I can’t get the function to list the content of a directory when it find one. I have been staring at this problem for hours but I can’t see the flaw.

    A nugde in the right direction would be gratefully apprepricated

    Regards

    Hmm

    The code:

    #include <iostream>
    #include <fstream.h>
    #include <windows.h>
    #include <string.h>
    using namespace std;

    bool FindFiles(char *pFiles,HANDLE handle);

    int main(){

    char *dir = "C:\*";
    HANDLE hFile;

    FindFiles(dir,hFile);
    return 0;
    }

    bool FindFiles(char *pFiles,HANDLE handle){

    WIN32_FIND_DATA fData;
    char buf[200],TmpPath[MAX_PATH],Buffer[MAX_PATH],*pBuffer;
    pBuffer=Buffer;


    if ((handle = FindFirstFile(pFiles,&fData))==INVALID_HANDLE_VALUE){
    wsprintf (buf, "Error code %d", GetLastError() );
    return 0;
    }


    while (FindNextFile(handle,&fData)!=0){
    if((fData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)!=0 && strcmp(fData.cFileName,"..")){
    GetCurrentDirectory(MAX_PATH,TmpPath);
    FindFiles(strcat(TmpPath,"\\*.txt"),handle);
    }

    strcpy(Buffer,fData.cFileName);
    std::cout << pBuffer << std::endl;

    }
    FindClose(handle);
    return 0;
    }

  2. #2
    Member
    Join Date
    Feb 2001
    Posts
    57

    How about this...

    I made a few changes and this should now display the files and folders in C:\. You will need to play around with the code to get the recursion effect that you want however.

    PHP Code:

    #include <iostream.h> 
    #include <fstream.h> 
    #include <string.h> 
    #include <windows.h>
     

    bool FindFiles(char *pFiles,HANDLE handle); 

    int main(){ 

    char *dir "C:\\*.*"
    HANDLE hFile=0

    FindFiles(dir,hFile); 
    return 
    0


    bool FindFiles(char *pFiles,HANDLE handle){ 

    WIN32_FIND_DATA fData
    char buf[200],TmpPath[MAX_PATH],Buffer[MAX_PATH],*pBuffer
    pBuffer=Buffer


    if ((
    handle FindFirstFile(pFiles,&fData))==INVALID_HANDLE_VALUE){ 
    wsprintf (buf"Error code %d"GetLastError() ); 
    return 
    0



    while (
    FindNextFile(handle,&fData)!=0){ 
    if((
    fData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)!=&& strcmp(fData.cFileName,"..")){ 
    GetCurrentDirectory(MAX_PATH,TmpPath); 
    FindFiles(strcat(TmpPath,"\\*.txt"),handle); 


    strcpy(Buffer,fData.cFileName); 
    cout << pBuffer << endl


    FindClose(handle); 
    return 
    0


  3. #3

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Thanks chilibean,

    I'll let you know how I get on

    cheers

    hmm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width