Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to search the computer for files and folders

Tags:

c++

file

windows

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.

like image 508
blood Avatar asked Jul 29 '10 17:07

blood


People also ask

How can you quickly search for files and folders?

Open File Explorer and navigate to the folder you want to search within. Click in the search field. You should see a list of items from previous searches. Type a character or two, and items from previous searches that match your criteria will appear.

What are the two ways in searching a file or folder in computer?

1. Click on the Windows key and type "File Explorer," clicking the top app result that appears. 2. If you know which drive or folder the file you're looking for exists in, choose that location via the Quick Access menu on the left-hand side of the File Explorer screen.

How do I get a list of all files on my computer?

For Windows 10, follow these instructions: Hold the windows key and press "r," type in "cmd" and then press enter, type in "cd ../.." and then press enter, type in "tree" and then press enter. This will usually show all of the files on your hard drive.

What program is used to search files and folders?

File Explorer allows you to use Windows Search Explorer (by default) to help you find and view all of your files or folders in one place.


2 Answers

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

    }

}
like image 147
monoceres Avatar answered Oct 21 '22 10:10

monoceres


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.
like image 42
Jacob Avatar answered Oct 21 '22 08:10

Jacob