how to search the computer for files and folders

45,528

Solution 1

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);

    }

}

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

 #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);

    }

}
Share:
45,528
blood
Author by

blood

Updated on July 28, 2020

Comments

  • blood
    blood almost 4 years

    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.