Directory Search Algorithm

I am about to create an algorithm that will go through the entire alphabet of drive-letters, and return a vector of the drives it finds are open. After it gathers that data, it wil then search for a folder whos name is given as a string argument. This will be usefule for programs I want to collect information for me. EX: a program that will automatically copy files from my computer, to my external HDD when I plug it in, or a program to finds video game files and backs them up, etc......

This will take a while to make, but I thought I would ask you guys if there was a winAPI, or Boost function that would shorten the length of time I spend writing this. IF YOU DO ANSWER ME, GIVING ME A FUNCTION, PLEASE EXPLAIN HOW IT WORKS, THE ARGUMENTS IT TAKES/RETURNS, AND HOW TO USE THOSE ARGUMENTS TO HANDLE THE DATA. <----(just making sure you see that)

I'm learning C++ myself (not in school, next semester though :( ) and would really appreciate any help you can give! :)

thank you for your time and effort!
Last edited on
There sure is, but it's not a simple function:

boost::filesystem will introduce you to some interesting classes and iterators that will let you iterate through as many of the files on a system as you want. You can then extract any data that you want using this library.
... go through the entire alphabet of drive-letters ...

No, this is not how to search for drives on a Windows machine. Use the "GetLogicalDriveStrings()" function given to you by M$:

From there you pass the drive letters to a function of your own which calls the "FindFirstFile()" and "FindNextFile()" functions to fill in the 'WIN32_FIND_DATA' structure. These should be in a different function so that you can call it recursively because if the dwFileAttributes member from 'WIN32_FIND_DATA' equals 16 then the file it represents is another folder.

- FindFirstFile():

- FindNextFile():


- File Attribute Constants:

Copying files is done with "CopyFile()":

The documentation on MSDN in the links I gave you is pretty good, please ask if you need anything cleared up.

I know about that, and I can not figure it out. As I said, please explain.


Thank you for your help, but it still is not enough. MSDN tells me how to write them, and how windows handles them, but not how I'M supposed to handle them. EX: in "GetLocigalDriveStrings()"

DWORD WINAPI GetLogicalDriveStrings(
_In_ DWORD nBufferLength,
_Out_ LPTSTR lpBuffer

I understand what the return value is, but not how to handle it. Is it a vector, array, or string? Is it even a variable? Where do I get the values to put in as arguments for the function?

I do not use the windows API unless I'm creating, or deleting folders/files. I have no experience in handling it. Is this 'buffer' some sort of another winAPI function that I call to retrieve the values in it?

Edit: it just occured to me that the functiuon is declared as a 'dword', so I will assume it returns a double. Please correct me if I'm wrong. Also, what is LPSTR?
Last edited on
GetLogicalDriveStrings is not a function that a beginner could easily understand due to the mode that fills lbBuffer parameter (a series of null-terminated strings, ending with another nul character).

You should check return value and, if it is greater that buffer size (which you need to allocate yourself), call the function again after allocate more memory.

A DWORD (double word) is not a double, it is usually an int.
You're not helping (except for the last part). I'm not a beginner. I just know nothing about useing the windows' API. I have to start somewhere, and why not here?

I guess I'll just write my algorithm... Would have been nice to get a bit more familiar with the API. w.e.
Last edited on
modoran just wanted you to know you shouldn't feel discouraged if it doesn't click right away, he also meant that you were a beginner with the WinAPI which is something I remember you admitting to not so long ago in another thread. Writing for Windows isn't completely parallel with writing in C\C++ like it is with other API's you might have used, in fact Microsoft has it's own implementation of the C Run Time Library (called MSCRT).

Also, you solved slightly less then a third of your problem and you're going to mark the thread complete? Are you confident in your ability to use the other functions? Or did you think we're going to abandon you because you rejected one part of our solution?

Yes, I am completely confedent. It took about 3-4 hours of work, on the algorithm and the code (didnt use winAPI, but did use boost for file/directory existence checking), and about 2-3 hours debugging it (as I have to make it completely moron proof... ), but it works like a charm! All you have to do is set it up using a menu that will list the drives it has 'detected'. You can refresh the menu if you have not plugged in your flash drive. Once you select a drive, it will create a directory under the drive name, and a keyfile it will use to identify that directory (otherwise, anyone can create the directory and exploit the program). After that is complete, you can use the program without the flash drive, or w.e., and when you want to transfer data you can initiate the transfer mode. It will constantly look for your directory, and pressing any key will stop this process. Once you plug it in, it will see the directory, read the keyfile, and if the keyfile matches, it formats your data in an easy-to-read format, and tells you when it is done copying the data to your flashdrive. You can even name the file the data is stored in so that you don't overwrite somthing you want to save (if you saved data from the program already with the same filename).

If you want the source code, the entire program is over 1k lines long. The algorithm is pretty darn large (100-200 lines).
Topic archived. No new replies allowed.