Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I am working on a project named "file crawler" in visual c++. It will simply index all the files of a specified directory in a file. The user can search this c++ file for a particular file through extension, name etc.

Right now, I want to get all the file names and directories into an object of my created class. I found this code helpful:

C
using namespace System;
using namespace System::IO;

int main()
{

   // Make a reference to a directory.

DirectoryInfo^ di = gcnew DirectoryInfo( "d:\\" );

   // Get a reference to each file in that directory.

array<FileInfo^>^fiArr = di->GetFiles();

   // Display the names of the files.

Collections::IEnumerator^ myEnum = fiArr->GetEnumerator();

 while ( myEnum->MoveNext() )
   {
      FileInfo^ fri = safe_cast<FileInfo^>(myEnum->Current);

 Console::WriteLine( fri->Name );
   }
}


The problem is that this code uses the file info handle and I don't know how to transfer all this data to an object that will be a pointer in a directory class (my class).

My class is like this:

C
class directories
{
    //data members
    ffile *file;  //file is my class
    ddirectory *sdirectory //ddirectory will hold the names of subdirectories

    string path;
};


I also need the explanation of:

Collections::IEnumerator^ myEnum = fiArr->GetEnumerator();


I am doing second year of graduation. Please suggest some ideas to me. I have 20 days to complete this project.
Posted
Updated 11-Mar-11 2:18am
v3

1 solution

I believe you may find this interesting:
http://www.boost.org/doc/libs/1_46_0/libs/filesystem/v3/doc/tutorial.html[^] - it will even work on other os'es

You can also use
http://www.boost.org/doc/libs/1_46_0/libs/regex/doc/html/index.html[^] to facilitate quite advanced search criteria.

The above is more of a "real" C++ approach than using C++/CLI, but if you want to use C++/CLI the following articles should help you to solve your problem in C++/CLI:
C++: The Most Powerful Language for .NET Framework Programming[^]
A Baker's Dozen: Thirteen Things You Should Know Before Porting Your Visual C++ .NET Programs[^]
Best Practices for Writing Efficient and Reliable Code with C++/CLI[^]

There is also the product documentation[^]

If you are new to C++, take a look at:
Thinking in C++ 2nd Edition by Bruce Eckel[^]

The links will allow you to solve your problem, but you're the one who is going to create your solution.

Good luck :)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sandeep Mewara 11-Mar-11 14:15pm    
Good answer with lots of reference! 5+
Espen Harlinn 11-Mar-11 17:06pm    
Thanks Sandeep :)
fjdiewornncalwe 11-Mar-11 14:32pm    
Awesome links. +5
Espen Harlinn 11-Mar-11 17:07pm    
Thanks Marcus :)
Nuri Ismail 12-Mar-11 12:03pm    
Excellent references! 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900