OK, so I need to write this function capable of traversing a directory and retrieving its files (and properties) grouped in subdirectories (if the case).
I will use the stuff retrieved to populate a MySQL table, but the MySQL-part is the simplest ingredient of the task.
I will highly appreciate if you point me to a tutorial or a code snippet.
[C++] Given a directory, retrieve all files and subdirectories?
no special library needed. just use FindFirstFile() and FindNextFile() functions;
FindFirstFile() takes the arguments: pointer to a string with the file name you want, and a file data structure.
simply to use
WIN32_FIND_DATA FileData;
char file[MAX_PATH];
HANDLE hnd;
DWORD attribute;
int error;
//fill in the file array with the directory in question
strcpy(file, "c:\\mydirectory\\");
//add the wildcard search
strcat(file, "*");
attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN;
hnd = FindFirstFile(file, %26amp;FileData);
if (hnd != INVALID_HANDLE_VALUE)
{
__if ((FileData.dwAttributes %26amp; attribute) = FILE_ATTRIBUTE_DIRECTORY)
_____//FileData.cFileName contains the subdirectory name
__do
__{
____error = FindNextFile(hnd, FileData);
____if (error != 0)
____{
______if ((FileData.dwAttributes %26amp; attribute) = FILE_ATTRIBUTE_DIRECTORY)
________//FileData.cFileName contains the next subdirectory name
____}
__}
__while (error != 0);
}
FindClose(hnd);
//now get the files in the directory
strcat(file, ".*");
hnd = FindFirstFile(file, %26amp;FileData);
if (hnd != INVALID_HANDLE_VALUE)
{
__//FileData.cFileName contains the name of the file
__do
__{
____error = FindNextFile(hnd, FileData);
____if (error != 0)
____{
______//FileData.cFileName contains the next file name
____}
__}
__while (error != 0);
}
FindClose(hnd);
NOTE: the properties can be retrieved through the FileData structure.
Reply:C++ does not support what you are trying to do. But it's your lucky day, I guess. The good people at Boost.org have helped you out with a library. It is open source, so have fun.
Reply:No offense but the guy who said C++ doesnt support that
feature is a moron. (sorry = / )
I would highly recommend just using someone elses
libraries for now.
Google is your friend on this one.
- Void
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment