blood
blood

Reputation: 746

how to search the computer for files and folders

i need a way to search the computer for files like Windows Explorer. i want my program to search lets say hard drive c:. i need it to search C:\ for folders and files (just the ones you could see in c:\ then if the user clicks on a file on the list like the folder test (C:\test) it would search test and let the user see what files/folders are in it.

Upvotes: 15

Views: 47922

Answers (6)

monoceres
monoceres

Reputation: 4770

Since you mentioned windows, the most straight forward winapi way to do it is with FindFirstFile and FindNextFile functions.

edit: Here's an example that shows you how to enumerate all files/folders in a directory.

#include <Windows.h>
#include <iostream>


int main()
{
    WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile(L"C:\\*",&file);
    if (search_handle)
    {
        do
        {
            std::wcout << file.cFileName << std::endl;
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);

    }

}

Upvotes: 18

Brackets
Brackets

Reputation: 572

There really is no need to use 3rd party library to accomplish this. This is a short, independent function which lists all files (with their paths) in a directory, including subdiretories' files. std::string folderName has to finish with \, and if you want to list all files on computer, just create a loop in calling function along with GetLogicalDriveStrings (It returns strings with \, so it couldn't be more convenient in this case).

void FindAllFiles(std::string folderName)
{
    WIN32_FIND_DATA FileData;
    std::string folderNameWithSt = folderName + "*";
    HANDLE FirstFile = FindFirstFile(folderNameWithSt.c_str(), &FileData);

    if (FirstFile != INVALID_HANDLE_VALUE) {
        do {
            if (strcmp(FileData.cFileName, ".") != 0 && strcmp(FileData.cFileName, "..") != 0)
            {
                if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    std::string NewPath = folderName + FileData.cFileName;
                    NewPath = NewPath + "\\";

                    FindAllFiles(NewPath);
                }
                else
                {
                    std::cout /*<< folderName*/ << FileData.cFileName << std::endl;
                }
            }
        } while(FindNextFile(FirstFile, &FileData));
    }
}

This is ASCII version, remember that files and folders can be named in Unicode

Upvotes: 2

Elixir Techne
Elixir Techne

Reputation: 1856

 #include <Windows.h>
#include <iostream>


int FindF(char* pDirectory)
{
    char szFindPath[MAX_PATH] = {0};
    strcpy(szFindPath, pDirectory);
    strcat(szFindPath, "\\*");
    WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile(szFindPath,&file);
    if (search_handle)
    {
        do
        {
            if(file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
            {
              strcpy(szFindPath, pDirectory);
              strcat(szFindPath, "\\");
              strcat(szFindPath, file.cFileName);
              FindF(szFindPath);
            }
            std::wcout << file.cFileName << std::endl;
        }while(FindNextFile(search_handle,&file));
        CloseHandle(search_handle);

    }

}

Upvotes: 2

Amardeep AC9MF
Amardeep AC9MF

Reputation: 19044

You can use Directory class members to do this with C# or managed C++. See the following MSDN article:

http://support.microsoft.com/kb/307009

If you wish to use C++ with MFC you can use CFileFind

http://msdn.microsoft.com/en-us/library/f33e1618%28v=VS.80%29.aspx

You'll have to supply your own browse window to present the file system tree.

Or you can use one of the directory/file controls to do both for you.

Upvotes: 2

Jacob
Jacob

Reputation: 34601

This will be OS dependent. The SO question

How can I get a list of files in a directory using C or C++?

handles this problem well. You can download DIRENT here.

Now that you have this, I'd recommend recursively searching for a file with a DFS/BFS algorithm. You can assume the whole directory structure is a tree where each file is a leaf node and each subdirectory is an internal node.

So all you have to do is,

  1. Get the list of files/folders in a directory with a function such as:
    void getFilesFolders(vector<string> & dir_list, const string & folder_name)
  2. If it's a directory, go to 1 with the directory name
  3. If it's a file, terminate if it's the file you're looking for, else move on to the next file.

Upvotes: 6

Ben Usman
Ben Usman

Reputation: 8387

boost::filesystem can be a cross-platform solution for that (check out for such functions in it).

Upvotes: 2

Related Questions