tags:

views:

65

answers:

4

Hi I'm studying this book (Addison Wesley Windows System Programming 4th Edition) and I think its useless Im working on a searching code that support the recursive so it can go in deepth in files and directories the code is working ( I guess ) no syntax error but the output is not what I want the out put of the search is like:

    not found
Now, here are the folders:
not found
Searching in d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\..\e-books\.\.\.\.\E-BOOKS
The file name is: d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\..\e-books\.\.\.\.\E-BOOKS\*Test*
not found
Now, here are the folders:
not found
Searching in d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\..\e-books\.\.\.\..
The file name is: d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\..\e-books\.\.\.\..\*Test*
not found
Now, here are the folders:

First I notiiced that what ever I do it will not search just inside the folder i specified but in all whole drive and the second annoying probem is the DOTS the . and .. those appear in each folder how can I avoid this problem. now as i said before Im using the book I mentioned before but I dont know I just dont like what i did is there a better way to form my code .

the code :

#include "stdafx.h"
#include <windows.h>

void SearchForFile(TCHAR *folder, TCHAR *file){
    _tprintf(L"Searching in %s\n",folder); //just to show the state
    TCHAR temp[1000];

    _stprintf(temp,L"%s\\%s",folder,file); // here  wrote into temp the location as folder/file
    _tprintf(L"The file name is: %s\n",temp);
    HANDLE f;
    WIN32_FIND_DATA data;
    f=FindFirstFile(temp,&data);
    if(f==INVALID_HANDLE_VALUE){
        _tprintf(L"not found\n");

    }
    else{
        _tprintf(L"found this file: %s\n",data.cFileName);
        while(FindNextFile(f,&data)){
            _tprintf(L"found this file: %s\n",data.cFileName);
        }
        FindClose(f);   
    }

    _stprintf(temp,L"%s\\*",folder); // "d:\*" for example
    _tprintf(L"Now, here are the folders:\n");
    f=FindFirstFile(temp,&data);
    TCHAR temp2[1000];
    if(f==INVALID_HANDLE_VALUE){
        _tprintf(L"not found\n");

    }
    else{
        if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
            {

            //_tprintf(L"found this directory: %s\n",data.cFileName);
                _stprintf(temp2,L"%s\\%s",folder,data.cFileName);
                SearchForFile(temp2,file);
            }
        while(FindNextFile(f,&data)){//         _tprintf(L"%d   %d\n",data.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY);
            if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
            //  _tprintf(L"found this directory: %s\n",data.cFileName);
            {
                _stprintf(temp2,L"%s\\%s",folder,data.cFileName);
                SearchForFile(temp2,file);

            }
        }
        FindClose(f);   
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    SearchForFile(L"d:\\test", L"*Test*");
    return 0;
}
+3  A: 

You have to filter out the . and .. pseudo-folders found in every folder.
Roughly, in your recursive branch:

if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 
     && data.data.cFileName != "." 
     && data.data.cFileName != "..")
Henk Holterman
Oh I see thanx ,,, but what about the code do you think there is a way to Improve it I my self not convinced (My first time) or do you think its fine ? is there a way to make the recursivity better
kristian Roger
Your code is largely OK, for production code I would add some more errorchecking. For example inside the wile-loops (what if somebody pulls the USB stick while you're reading its folders?)
Henk Holterman
kristian Roger
kristian Roger
@krstian, it is perfectly legal to have a `.Test` folder. You would skip that. Don't optimize prematurely.
Henk Holterman
A: 

Pretty much no matter how you find the contents of a directory on Windows the first matches will be '.' (the current directory) and '..' (the parent directory). You probably want to ignore both of them.

tloach
A: 

Usually you explicitly test for and skip the "." and ".." subdirectories that are present in all directories (but the root). The code you're using searches subdirectories recursively, and since you're not ignoring the ".." directory, it'll search that, which will eventually lead to the root directory, and search all subdirectories from there -- meaning it'll search the whole disk.

Jerry Coffin
+1  A: 

In general, you should skip "." and ".." directories, they are synonyms for "current" and "parent" directory.

Axarydax