Problems with searching in files and directories .. windows programming

I am studying this book (Addison Wesley Windows System Programming 4th Edition) and I think that its useless Im is working on search code that supports recursive, so it can go in depth in files and directories, the code works (I think) there is no syntax error but the output is not what I want to display the search:

    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:

At first I didn’t notice that someday I would do it, it will not search only inside the folder indicated by me, but in the whole disk, and the second annoying probe is DOTS. and ... they appear in every folder, how can I avoid this problem. Now, as I said before I used the book that I mentioned before, but I don’t know that I just don’t like what I did, this is the best way to build my 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
4

. .., .
, :

if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 
     && data.data.cFileName != "." 
     && data.data.cFileName != "..")
+5

, ".". "..", "" "" .

+1

, , Windows, "." ( ) ".." ( ). , , .

0

Usually you explicitly check and skip the ".". and "..", which are present in all directories (but in the root). The code you use looks for subdirectories recursively, and since you do not ignore the ".." directory, it will look for this, which will ultimately lead to the root directory and will search for all subdirectories from there - ll search for the entire disk.

0
source

Source: https://habr.com/ru/post/1742504/


All Articles