tags:

views:

110

answers:

5

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.

+2  A: 

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.

Amardeep
That uses .Net and requires using C++/CLI.
Georg Fritzsche
@Georg: Thanks for the clarification. I've added additional information in case he wants to use C++ without .NET.
Amardeep
:| i don't see the info
blood
@blood: Maybe hit the refresh button. I added a line about CFileFind and a link to the MSDN doc page.
Amardeep
i did >_> it did not refresh i guess
blood
i keep getting errors on all of the code. i don't seem to have the headers i need. i don't have mfc :| i think that might be why all of your code wont compile.
blood
@blood: What is your build platform? Are you using one of the Visual Studio IDE's?
Amardeep
+2  A: 

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.
Jacob
How can I get a list of files in a directory using C or C++? is good but the code i can't get to work. only Brian R. Bondy's code the very last one of his but it only finds files part of what i wanted and also i can't find how it works so i can't edit it for my program :(.
blood
playing with dirent to see if it works get back to you on that
blood
uhh i can't get dirent i download it but it unzips as a .9 file and thats all. did i miss something because i just downloaded it from your link
blood
http://www.softagalleria.net/download/dirent/dirent-1.9.zip --- Maybe the .9.zip is confusing your unzip program. I can see a whole VC project here.
Jacob
The VC project at that link also has a demo file which prints the contents of a directory.
Jacob
+2  A: 

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));
        CloseHandle(search_handle);

    }

}
monoceres
this only finds files right? i need folders and files.
blood
No it finds both. It also allows you to give a search criteria for finding files/folders.
monoceres
I added an example as well.
monoceres
:D it seems to work tyvm man.
blood
A: 

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

MInner
i did not really want 3rd party software
blood
A: 
 #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);

    }

}
Trinity