English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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.

2007-01-22 04:43:46 · 4 answers · asked by Bogdan 2 in Computers & Internet Programming & Design

4 answers

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, &FileData);
if (hnd != INVALID_HANDLE_VALUE)
{
__if ((FileData.dwAttributes & attribute) = FILE_ATTRIBUTE_DIRECTORY)
_____//FileData.cFileName contains the subdirectory name
__do
__{
____error = FindNextFile(hnd, FileData);
____if (error != 0)
____{
______if ((FileData.dwAttributes & 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, &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.

2007-01-23 02:03:10 · answer #1 · answered by justme 7 · 0 1

==== First, significant notes a million) maximum critically, use "xcopy". It has greater helpful thoughts. 2) additionally, if the /holiday spot/ itemizing would not yet exist, then upload the "/i" change. This tells xcopy to think of that the holiday spot is a itemizing once you're copying greater advantageous than one record, and it will create the holiday spot dir for you. ==== 2d, some concepts, a million) upload the "/h" change, in case you elect to get hidden and equipment archives, too. 2) upload the "/e" which copies dirs and sub-dirs at the same time with empties. 3) upload the "/c" change. this might make the xcopy command proceed to added archives/dirs in spite of if it hits an blunders. this way, you will get maximum stuff, then be waiting to be sure why it particularly is not getting something. 4) in case you elect to get all the stuff decrease than source, and you're applying the "/e" change, then the *.* isn't required. 5) once you are going to try this greater advantageous than as quickly as, and you elect to verify to overwrite study-purely archives on your holiday spot, then upload the "/r" change. 6) in case you do no longer choose to work out the "overwrite ? (confident,No, All)" instantaneous every time you run your batch, then substitute your "/-y" to a "/y" =========== So, your commandline ought to look something like, xcopy D:dir1 C: /e /h /c /i /y /r

2016-12-12 17:38:33 · answer #2 · answered by Anonymous · 0 0

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.

2007-01-22 06:53:45 · answer #3 · answered by bayou64 4 · 0 2

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

2007-01-22 13:17:59 · answer #4 · answered by Hex 5 · 0 2

fedest.com, questions and answers