Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
I'm writing program whose purpose to count files in specified directory and when appear new file it should get this file name and store it. My question is how could i do it? :O

PS: My OS is Windows7, also i have installed boost c++ libs.
Posted

It is certainly possible to do for Windows. A very good answer is on the top of this Stackoverflow question page: http://stackoverflow.com/questions/931093/how-do-i-make-my-program-watch-for-file-modification-in-c[^].

Unfortunately, I don't know a boost solution, and the only cross-platform solution is in Trolltech's Qt.

—SA
 
Share this answer
 
v2
You're looking for change notifications. See the example at http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261%28v=vs.85%29.aspx[^]

Hope that helps.
 
Share this answer
 
I had to figure out the same thing a couple years ago. I read about / tried a bunch of different APIs. I ended up using the ReadDirectoryChangesW API. All of the other methods had problems of some sort.
 
Share this answer
 
v2
@Harley L. Pebley
Already found it before, but i don't really know how to adopt it in the code
There's a piece of my code, loop which is checking for new files in directory.
If any of You guys, could edit this and apply function which we talking about to this, i will be very glad.
C++
while (true)
{
    if (count_files(path, ".png") > files_amount)
    {
        cout << "New image file has been created" << endl;

        files_amount = files_amount + 1;

        cout << "Current images count: " << files_amount << endl;


    }
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Jul-14 18:50pm    
Harley won't get a notification from you. Move it to the comment to Harley's answer and update your question using "Improve question". If posted as "Solution", such posts are considered as abuse.
—SA
Harley L. Pebley 15-Jul-14 19:08pm    
If you look at the example I linked to, the WatchDirectory method has a while(true) loop in it. That loop has places where you can add your own behavior as indicated by the comments. The code above the loop is needed as setup to use the API methods.

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