how looking for a folder?

i have these function for show a folder name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int FindF(char* pDirectory)
{
    char* startpDirectory=pDirectory;
    char szFindPath[MAX_PATH] = {0};
    strcpy(szFindPath, startpDirectory);
    strcat(szFindPath, "\\*");
    WIN32_FIND_DATA file;
    //file.dwFileAttributes= FILE_ATTRIBUTE_DIRECTORY;
    HANDLE search_handle=FindFirstFile(szFindPath,&file);
    if (search_handle)
    {
        do
        {
            if(file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY || FILE_ATTRIBUTE_SYSTEM)
            {
              strcpy(szFindPath, startpDirectory);
              strcat(szFindPath, "\\");
              strcat(szFindPath, file.cFileName);
              //Findf(szFindPath);
              std::wcout << file.cFileName << std::endl;
            }

        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);

    }

}

but i see some problems:
1 - i can't see subfolders;
2 - can i, too, print the entire folder path?
memset(&file, 0, sizeof(file));

and properly filling the WIN32_FIND_DATA may help ...
What are trying to do on line 14? This way it is certainly wrong.

1 - i can't see subfolders;
See this example:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx
Yes coder777, I haven't reached that far :-)

if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || file.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ...

i'm sorry, but i only get the folder and file name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int FindF(char* pDirectory)
{
    char* startpDirectory=pDirectory;
    char szFindPath[MAX_PATH] = {0};
    strcpy(szFindPath, startpDirectory);
    strcat(szFindPath, "\\*");
    WIN32_FIND_DATA file;
    //file.dwFileAttributes= FILE_ATTRIBUTE_DIRECTORY;
    HANDLE search_handle=FindFirstFile(szFindPath,&file);
    if (search_handle)
    {
        do
        {
            if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || FILE_ATTRIBUTE_SYSTEM)
            {
              strcpy(szFindPath, startpDirectory);
              strcat(szFindPath, "\\");
              strcat(szFindPath, file.cFileName);
              //Findf(szFindPath);
              std::wcout << file.cFileName << std::endl;
            }

        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);

    }
}

- why i only get the folder name? i must add the 'startpDirectory' for get the entire folder name?
- can i avoid the file name?
yes i found 1 problem:
1
2
3
4
5
6
7
8
if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || FILE_ATTRIBUTE_SYSTEM)
            {
              strcpy(szFindPath, startpDirectory);
              strcat(szFindPath, "\\");
              strcat(szFindPath, file.cFileName);
              //Findf(szFindPath);
              std::wcout << file.cFileName << std::endl;
            }

to these:
1
2
3
4
5
6
if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || FILE_ATTRIBUTE_SYSTEM)
            {
              strcpy(szFindPath, startpDirectory);
              strcat(szFindPath, file.cFileName);
              std::wcout << szFindPath << std::endl;
            }

so yes.. i must add the folder name with drive name.
now how can i avoid the file name?
It is still wrong. See the what ocreus said:

if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || file.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)

Actually FILE_ATTRIBUTE_SYSTEM seems not to make sense?
msdn wrote:
A file or directory that the operating system uses a part of, or uses exclusively.

https://docs.microsoft.com/en-us/windows/desktop/FileIO/file-attribute-constants


If you use windows stuff you may consider to use things like _makepath, _splitpath, or _fullpath:

https://msdn.microsoft.com/en-us/library/sh8yw82b.aspx
https://msdn.microsoft.com/en-us/library/e737s6tf.aspx
https://msdn.microsoft.com/en-us/library/506720ff.aspx
you have right.. i did more errors.
but if i need the subfolders, how can i add them?
Last edited on
What do you mean by 'i need the subfolders, how can i add them?'?
Do you want to create subfolders?

If you want to search in the subfolders you can recursively call FindF(...) for a directory.
i understand do loops between loops, but in these case is like infinite loops, so i don't know how... can you teach me more?
i did, but it's an infinite loop :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void FindF( const char *FindDirectory, const char *StartDirectory = "C:\\")
{
    char szFindPath[MAX_PATH] = {0};
    strcpy(szFindPath, StartDirectory);
    strcat(szFindPath, "\\*");
    WIN32_FIND_DATA file;
    file.dwFileAttributes= FILE_ATTRIBUTE_DIRECTORY;
    HANDLE search_handle=FindFirstFile(szFindPath,&file);
    if (search_handle)
    {
        do
        {

                if (IsFileName(file.cFileName))
                    continue;
              strcpy(szFindPath, StartDirectory);
              strcat(szFindPath, file.cFileName);

              std::wcout << szFindPath << std::endl;
            FindF(FindDirectory,szFindPath);//after show the 1st folder name, i call it
        }while(FindNextFile(search_handle,&file));
    CloseHandle(search_handle);

    }
}

after show the 1st folder name, i called the function again, but i only the 1st folder name :(
the loop is infinite, can you explain more?
What does this IsFileName(...) do?

Line 9: You need to check against INVALID_HANDLE_VALUE

What is this FindDirectory? You do nothing with it.

I suggest what ocreus already stated:
http://www.cplusplus.com/forum/windows/238743/#msg1065051
memset(&file, 0, sizeof(file));

Apart from this I don't see why it would go inifinite. It may go a long time depending on the number of files within the directory. I suggest that you make your own directory not 'C:' to start from.
finally it's working like i need:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
string FindDirectory(std::string FindDirectoryName[2], std::string StartDirectory="C:")
{
    WIN32_FIND_DATA file={0};
    string str=StartDirectory+ "\\*"; //we must add the backslash with asterisc
    HANDLE search_handle = FindFirstFile(str.c_str(), &file);
    static bool blnFindDirectoryName=false;
    static string strFolderName ="";

    //testing if the folder is valid:
    if (search_handle != INVALID_HANDLE_VALUE)
    {
        do
        {
            //if the folder was founded, then break the loop:
            if(strFolderName!="")
                break;

            //for avoid files names and the folders "." and "..":
            if ((file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(file.cFileName, "..") && strcmp(file.cFileName, "."))
            {
                //cout <<StartDirectory + "\\" + file.cFileName << endl;

                //testing if the folder was found
                //and add it on static strFolderName
                if(string(file.cFileName)==FindDirectoryName[0] && blnFindDirectoryName==false)
                {
                    if(FindDirectoryName[1]=="")
                    {
                        //the entire folder path must be startdirectory more backslash and then forlder name
                        strFolderName = StartDirectory+ "\\" + file.cFileName; 
                        break;
                    }
                    else
                        blnFindDirectoryName=true;
                }
                if(string(file.cFileName)==FindDirectoryName[1] && blnFindDirectoryName==true)
                {
                    //MessageBox(NULL, string(StartDirectory+ "\\" + file.cFileName).c_str(), "found it", MB_OK);
                    strFolderName = StartDirectory+ "\\" + file.cFileName;
                    break;
                }
                else
                {
                    //or continue searching:
                    FindDirectory(FindDirectoryName,StartDirectory + "\\" + file.cFileName);
                }
            }
        } while (FindNextFile(search_handle, &file) && search_handle!=NULL);
        //destroy the search_handle:
        FindClose(search_handle);
    }
    //return the search folder full path:
    return strFolderName;
}

//testing:
string str2[2]={"mingw64","bin"};//2 strings(folder and subfolder) for have sure that found the right program folder
    cout << FindDirectory(str2);

now i can search a program more easy ;)
thank you so much for all.. thank you
Last edited on
Topic archived. No new replies allowed.