sr.
sr.

Reputation: 11

System::IO::Directory::GetFiles in c++

I am having trouble in storing the files in a string array from a directory in c++, using System::IO::Directory::GetFiles in c++

Also would like to know if we could copy an entire folder to a new destination/ in c++ like given in http://www.codeproject.com/KB/files/xdirectorycopy.aspx for c#

Upvotes: 1

Views: 4795

Answers (3)

Billy ONeal
Billy ONeal

Reputation: 106549

Assuming you're on Win32, you're looking for the FindFirstFile and FindNextFile APIs.

C/C++ does not define a standard way to do this, though Boost::Filesystem provides a method if you need cross platform support.

Upvotes: 0

user113476
user113476

Reputation:

You can store the file names from a directory in a managed array like this:

System::String ^path = "c:\\";  
cli::array<System::String ^>^ a = System::IO::Directory::GetFiles(path);

Console::WriteLine(a[0]);
Console::ReadKey();

As for how would you copy an entire folder... Simply recurse from a given root directory creating each directory and copying the files to the new location. If you are asking for code for this, then please say so, but at least try to figure it out for yourself first (i.e. show me what you have so far).

Upvotes: 1

MSalters
MSalters

Reputation: 179819

Check out the file listing program in Boost::FileSystem: http://www.boost.org/doc/libs/1_41_0/libs/filesystem/example/simple_ls.cpp. They iterate over all files, printing the paths, but it's trivial to store them instead.

Upvotes: 0

Related Questions