question on searching for a file in directory tree - Programmer

This is a discussion on question on searching for a file in directory tree - Programmer ; hello everyone, I have been going through the posts in this group to see if I could find the answer to my question(s) before I asked them but unfortunately I was not able to. So I am asking about a ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: question on searching for a file in directory tree

  1. question on searching for a file in directory tree

    hello everyone,

    I have been going through the posts in this group to see if I could
    find the answer to my question(s) before I asked them but unfortunately
    I was not able to. So I am asking about a method for searching for a
    file that allows you to search through sub-directories.

    What I have so far has allowed me to find a file in a specified
    directory with success. I have found a couple of ways of doing this.
    Now, I'd like to be able to be able to search for a file in the current
    directory and if it is not found, then search through any
    sub-directories.

    Understand the the method of creating a HANDLE variable and a
    WIN32_FIND_DATA variable to use the FindFirstFile() function to find a
    file. I also understand about testing the WIN32_FIND_DATA vaiable's
    dwFileAttributes field for the flag of FILE_ATTRIBUTE_DIRECTORY. I have
    seen in other posts that people recomend to use this function during
    searching through sub-directories but what I am confused about is that
    if I am searching for file "test.txt", how do I use the FindFirstFile()
    function on going down levels of the directory tree? Doesn't the FFF
    function only return a value if the file name specified does not match?
    Here is an example of what I am confused about:

    Variables:
    WIN32_FIND_DATA w32Data;
    HANDLE hFileName = INVALID_HANDLE_VALUE;//initialised handle
    Directory Tree:
    DIR Toplevel
    |
    |->FILE MyProgram.exe
    |
    |->SUBDIR FirstSub
    | |
    | |->FILE test.txt
    | |->FILE more.exe
    |
    |->SUBDIR SecondSub
    |
    |->FILE testing.txt

    Ok, this will be my directory tree setup. Now, I want to search for the
    file "test.txt". I use FindFirstFile(hFilename, &w32Data). Now
    shouldn't this call not find a file since there is no file in the
    Toplevel direcotry that matches "test.txt"?

    Thanks for any help on this confusion. I really appreciate any
    assistance.


  2. Re: question on searching for a file in directory tree

    enigma wrote:
    > I have been going through the posts in this group to see if I could
    > find the answer to my question(s) before I asked them but unfortunately
    > I was not able to. So I am asking about a method for searching for a
    > file that allows you to search through sub-directories.
    >
    > What I have so far has allowed me to find a file in a specified
    > directory with success. I have found a couple of ways of doing this.
    > Now, I'd like to be able to be able to search for a file in the current
    > directory and if it is not found, then search through any
    > sub-directories.


    You just need to make your function recursive. So for example, if you
    have a function like LookForTestInDir(const char *dir), you can make it
    loop over files in that directory and when it encounters another
    directory it can call LookForTestInDir again on that directory.

    - Neil

  3. Re: question on searching for a file in directory tree

    But how do you know it has reached a directory within the directory you
    are looking for? I have done a simple test loop to see if i can
    determine if a directory or a file is found. I posted it below. The
    resulting output is:

    This is a Direcotyr named: .

    Where . is the name of the found directory and that is all that is
    outputed. Below is the code for the simple function that i am using to
    test.

    void
    MyFindFile( char *replacementA )
    {
    // Data structure describes the file found
    WIN32_FIND_DATA FileDataL;
    HANDLE hFileL = INVALID_HANDLE_VALUE,
    hSearchL = INVALID_HANDLE_VALUE; //file handles
    char tmpL[65536], fileL[128], dirL[128];

    ZeroMemory(tmpL,sizeof(tmpL));
    ZeroMemory(fileL,sizeof(fileL));
    ZeroMemory(dirL,sizeof(dirL));
    sprintf(dirL,"*.*");

    //finding the first file that matches the filename
    hSearchL = FindFirstFile( dirL, &FileDataL);

    //making sure that a proper search handle was found
    if( hSearchL == INVALID_HANDLE_VALUE )
    {
    cout << "\tError while searching!\n";
    return;
    }

    do {
    cout << "\n\t***TESTING DO LOOP***\n";
    if(FileDataL.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    cout << "This is a Directory named: " <<
    FileDataL.cFileName << endl;
    }
    else if(FileDataL.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
    {
    cout << "This file: " << FileDataL.cFileName <<
    " is a not a directory.\n";
    }
    else
    {
    cout << "This is just testing to see if anything was done.\n";
    }
    }while(!FindNextFile (hSearchL, &FileDataL) );
    }

    The first IF statement of the DO loop is entered and then there are not
    anymore matching files found. Yes I know that this won't search
    sub-directories, this is just to test that I can tell if something is a
    directory or not. Is there a better way?

    Thanks


  4. Re: question on searching for a file in directory tree

    > The first IF statement of the DO loop is entered and then there are not
    > anymore matching files found. Yes I know that this won't search
    > sub-directories, this is just to test that I can tell if something is a
    > directory or not. Is there a better way?


    The reason it only outputs one file is because you're using the return
    value from FindNextFile wrongly. It returns a non-zero value if there
    _is_ a file found, so you're while condition should become:

    }while(FindNextFile (hSearchL, &FileDataL) );

    instead of

    }while(!FindNextFile (hSearchL, &FileDataL) );

    To make it look in subdirectories, you then just need to make your
    function accept a directory name to search in as a parameter and then
    call the function again with the subdirectory name when one is found.
    You can make it return a value whenever the "test.txt" value is found
    and stop looking if either the function found it itself or one of it's
    recursive calls found it.

    - Neil

  5. Re: question on searching for a file in directory tree

    Ahhhh...Thanks I guess I was misunderstanding the values of the
    functions. Thanks alot


  6. Re: question on searching for a file in directory tree

    Ok now I just want some ideas. I was able to make my code search
    through the current directory and then any sub-directories. But that is
    as far as it goes. I can only get it to go down one level in the
    directory tree and I am not sure why.

    What I do is I search the current directory first then when there are
    no more files in the directory I do a loop to find a directory. The
    method used is the same as for finding a file except for the file name
    to search for is "*.*". Once I find a new directory I call the find
    file function again and add the name of the directory to the name of
    the file to search for.

    Here is an example:
    My function accepts a path name and a file name to search for. The
    original call to the function passes in the path name of *.* and the
    file name *.txt. I want to find all *.txt files.

    First I search the current directory by using FindFirstFile and pass it
    the file name "*.txt" and then use FindNextFile in a do/while loop. I
    get a print out of all of the *.txt files found. Once it does not find
    anymore files I do a new search and pass in the file name *.* into the
    FindFirstFile function and then enter a new do/while loop. I check the
    attributes of the files found for the DIRECTORY flag. If a directory is
    found then I call the original function and pass in the path as the new
    directory and leave the file name the same as before.
    The file name is then edited within the function to include the new
    directory name as well. For instance if the direcotry Debug was found
    then in the new function call the file to search for would be
    Debug\*.txt.

    So I am unsure as to what I am missing. Below is the code that I am
    using. I am sure that it is something simple that is right in front of
    my face. I hate it when that happens.

    void
    RecursiveDirectoryFileSearch( char *PathA, char *FileA, char *NameA )
    {
    WIN32_FIND_DATA PathDataL, FileDataL; // Data structure describes the
    file found
    HANDLE hSearchFileL = INVALID_HANDLE_VALUE, hSearchPathL =
    INVALID_HANDLE_VALUE; // handles used for referencing the files

    FILE *EditFileL = NULL;

    char fileL[128], dirL[128], tmpL[65536];
    ZeroMemory(dirL,sizeof(dirL));
    ZeroMemory(fileL,sizeof(fileL));

    hSearchFileL = FindFirstFile( FileA, &FileDataL );
    if( hSearchFileL != INVALID_HANDLE_VALUE ) {
    do {
    int i=0, count = 0;
    ZeroMemory(tmpL,sizeof(tmpL));
    sprintf(fileL,"%s\\%s",PathA,FileDataL.cFileName);
    int x = strcspn(fileL,"*");
    for(i=0;i fileL[x+i] = fileL[x+i+4];
    if(fileL[x+i] == NULL )
    break;
    }
    if( strcmp(FileDataL.cFileName,".") &&
    strcmp(FileDataL.cFileName,"..")) {
    cout << FileDataL.cFileName << endl;
    }
    }while( FindNextFile(hSearchFileL, &FileDataL) );
    }

    hSearchPathL = FindFirstFile( PathA, &PathDataL );
    if( hSearchPathL != INVALID_HANDLE_VALUE ) {
    do {
    if(( PathDataL.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
    strcmp(PathDataL.cFileName, ".") &&
    strcmp(PathDataL.cFileName, ".."))
    {
    sprintf(dirL,"%s\\%s",PathDataL.cFileName,PathA);
    sprintf(fileL,"%s\\%s",PathDataL.cFileName,FileA);
    RecursiveDirectoryFileSearch(dirL,fileL,NameA);
    }

    }while ( FindNextFile(hSearchPathL, &PathDataL ));
    }
    if(hSearchPathL != INVALID_HANDLE_VALUE)
    FindClose(hSearchPathL);
    if(hSearchFileL != INVALID_HANDLE_VALUE)
    FindClose(hSearchFileL);
    }


  7. Re: question on searching for a file in directory tree

    Ahh I think I figured out what the problem is, now to figure out how to
    fix it. It seems that when I create the new path name, the new
    found directory is added to the front, which would be incorrect since
    it is a child of the current directory.


+ Reply to Thread