c++: how can i search all folders and subfolders?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 2674 Views - Last Post: 02 July 2018 - 02:37 PM Rate Topic: **--- 3 Votes

#1 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 11:56 AM

i did these code for search the folders on a folder path:
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 (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                std::wcout << szFindPath << std::endl;
            }
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);
    }
}

the folder names and their path are printed.
but how can i search the sub folders?
someone advice me use a recursive way, but i don't understand how can i do it. can anyone explain to me?

Is This A Good Question/Topic? 0
  • +

Replies To: c++: how can i search all folders and subfolders?

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:01 PM

Between lines 17 and 18 call your FindF() recursively passing in the szFindPath as the new start directory.

As an aside, there's something strange with your line 17. In all your code, your using ANSI/Multibyte strings, but on line 17, you are using wcout which uses Unicode/Wide strings.

Also, your error checking on line 9 is incorrect. When FindFirstFile() fails, it will return INVALID_HANDLE_VALUE which is non-zero. You are simply checking for non-zero and assuming that the handle is valid.
Was This Post Helpful? 1
  • +
  • -

#3 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:07 PM

i did that:
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 (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                std::wcout << szFindPath << std::endl;
                FindF(FindDirectory,szFindPath );
            }
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);
    }
}

it will print, in an infinite loop, always the 1st folder... why?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:13 PM

Step through the code and find out.

Hint: Look closely for the directories named "." and "..".
Was This Post Helpful? 1
  • +
  • -

#5 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:22 PM

thank you so much:
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!=INVALID_HANDLE_VALUE)
    {
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                if(szFindPath==".." || szFindPath==".")
                    continue;
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                std::wcout << szFindPath << std::endl;
                FindF(FindDirectory,szFindPath );// i belive these folder path isn't completed because of the backslash
            }
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);
    }
}

but i'm doing another error with strings: seems that i'm not using the backslash on folder path. so the program, sometimes, prints with dot
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:33 PM

Two things:
First: You need to insert the backslashes when you build up a path or filename.

Second: see second paragraph on post #2:

View PostSkydiver, on 30 June 2018 - 03:01 PM, said:

As an aside, there's something strange with your line 17. In all your code, your using ANSI/Multibyte strings, but on line 17, you are using wcout which uses Unicode/Wide strings.

Was This Post Helpful? 1
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:42 PM

Also recall that you can't compare C strings like ==.

You also need to use FindClose() not CloseHandle() when closing the search handle.
Was This Post Helpful? 1
  • +
  • -

#8 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:42 PM

i did, but with some problems:
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!=INVALID_HANDLE_VALUE)
    {
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                if(szFindPath==".." || szFindPath==".")
                    continue;
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                strcat(szFindPath, "\\");
                std::cout << szFindPath << std::endl;
                FindF(FindDirectory,szFindPath );
            }
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);
    }
}

Attached Image
ok. i have 2 warnings, so on 'if', but maybe isn't the real problem here.
so why i get that print? did i miss something? for use a backslah i must add it a double backslash
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:45 PM

Come to think of it, why are you even checking szFindPath for "." and ".."?
Was This Post Helpful? 1
  • +
  • -

#10 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 12:55 PM

View PostSkydiver, on 30 June 2018 - 12:45 PM, said:

Come to think of it, why are you even checking szFindPath for "." and ".."?

i don't need that names\strings ;)
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 01:09 PM

But it's file.cFileName which will have the "." or ".." strings, not szFindPath.

Looks like you posted #8 at the same time I posted #7. I suggest looking at post #7 as well.
Was This Post Helpful? 1
  • +
  • -

#12 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 01:15 PM

you have right. that's why i fixed that too:
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!=INVALID_HANDLE_VALUE)
    {
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                if(strcmp(file.cFileName,"..")==0 || strcmp(szFindPath,".")==0)
                    continue;
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                strcat(szFindPath, "\\");
                std::cout << szFindPath << std::endl;
                FindF(FindDirectory,szFindPath );
            }
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);
    }
}

but the problem(from that image) remains :(

i did my friend.
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!=INVALID_HANDLE_VALUE)
    {
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                if(strcmp(file.cFileName,"..")==0 || strcmp(szFindPath,".")==0)
                    continue;
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                strcat(szFindPath, "\\");
                std::cout << szFindPath << std::endl;
                FindF(FindDirectory,szFindPath );
            }
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);
    }
}

i belive that i have 1 problem here:
if(strcmp(file.cFileName,"..")==0 || strcmp(szFindPath,".")==0)
                    continue;
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                strcat(szFindPath, "\\");
                std::cout << szFindPath << std::endl;
                FindF(FindDirectory,szFindPath );

because the print, from that image, isn't correct
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6287
  • View blog
  • Posts: 21,610
  • Joined: 05-May 12

Re: c++: how can i search all folders and subfolders?

Posted 30 June 2018 - 01:49 PM

You need the put the slash between the current directory you are looking at and the name of the directory found by FindFirst()/FindNext(). Set a breakpoint on line 6 notice that you are assembling the string incorrectly: start + name + '\\'. You want start + '\\' + name

Since you are writing C++ code, why are you using C strings? Did your performance testing reveal that C strings will be faster than C++ strings?
Was This Post Helpful? 1
  • +
  • -

#14 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 743
  • Joined: 04-March 12

Re: c++: how can i search all folders and subfolders?

Posted 01 July 2018 - 01:45 AM

i'm sorry, but i did it without sucess :(
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!=INVALID_HANDLE_VALUE)
    {
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //avoiding files
            {
                if(strcmp(file.cFileName,"..")==0 || strcmp(szFindPath,".")==0)
                    continue;
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                strcat(szFindPath, "\\");
                std::cout << szFindPath << std::endl;
                char szFindPath2[MAX_PATH] = {0};
                strcpy(szFindPath2, StartDirectory);
                strcat(szFindPath2, "\\");
                strcat(szFindPath2, file.cFileName);
                FindF(FindDirectory,szFindPath2 );
            }
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);
    }
}


but i get the same print from the image :(
Was This Post Helpful? 0
  • +
  • -

#15 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7197
  • View blog
  • Posts: 15,004
  • Joined: 16-October 07

Re: c++: how can i search all folders and subfolders?

Posted 01 July 2018 - 02:29 AM

You have a bug in your directory strcmp check.

Fixing that should actually yield results:
void FindF(const char *FindDirectory, const char *StartDirectory = "C:\\") {
    char szFindPath[MAX_PATH] = { 0 };
    strcpy(szFindPath, StartDirectory);
    strcat(szFindPath, "\\*");
    WIN32_FIND_DATA file;
    // pointless file.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
    HANDLE search_handle = FindFirstFile(szFindPath, &file);
    if (search_handle != INVALID_HANDLE_VALUE) {
        do {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                // if (strcmp(file.cFileName, "..") == 0 || strcmp(szFindPath, ".") == 0) { continue; }
                if (strcmp(file.cFileName, "..") == 0 || strcmp(file.cFileName, ".") == 0) { continue; }
                strcpy(szFindPath, StartDirectory);
                strcat(szFindPath, file.cFileName);
                strcat(szFindPath, "\\");
                std::cout << szFindPath << std::endl;
                char szFindPath2[MAX_PATH] = { 0 };
                strcpy(szFindPath2, StartDirectory);
                strcat(szFindPath2, "\\");
                strcat(szFindPath2, file.cFileName);
                FindF(FindDirectory, szFindPath2);
            }
        } while (FindNextFile(search_handle, &file));
        FindClose(search_handle);
    }
}



That said, I think the FindDirectory - StartDirectory this is confusing you. It is certainly confusing me. You should be able to get similar results with:
void FindF(const char *root) {
    char szFindPath[MAX_PATH];
    strcpy(szFindPath, root); strcat(szFindPath, "\\*");
    WIN32_FIND_DATA file;
    HANDLE search_handle = FindFirstFile(szFindPath, &file);
    if (search_handle != INVALID_HANDLE_VALUE) {
        do {
            if ((file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(file.cFileName, "..") && strcmp(file.cFileName, ".")) {
                strcpy(szFindPath, root); strcat(szFindPath, "\\"); strcat(szFindPath, file.cFileName);
                std::cout << szFindPath << std::endl;
                FindF(szFindPath);
            }
        } while (FindNextFile(search_handle, &file));
        FindClose(search_handle);
    }
}



It's still horribly old school C. So, for fun, with actual strings:
void FindF(const std::string &root) {
    std::cout << root << std::endl;
    WIN32_FIND_DATA file;
    HANDLE search_handle = FindFirstFile((root + "\\*").c_str(), &file);
    if (search_handle != INVALID_HANDLE_VALUE) {
        do {
            if ((file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(file.cFileName, "..") && strcmp(file.cFileName, ".")) {
                FindF(root + "\\" + file.cFileName);
            }
        } while (FindNextFile(search_handle, &file));
        FindClose(search_handle);
    }
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3