Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design a filewatcher /directory watcher in VC++?

I am new to VC++ and programming. I have a task in which I am supposed to design a file watcher in VC++.

The problem goes this way:

I have to monitor some log files continously; whenever a particular log file gets deleted(this deletion is done by some other program), I have to open a TextFile and write some data and the timestamp into it.

How do I go about it? Please help!!

like image 559
Dolly Avatar asked Dec 03 '25 06:12

Dolly


1 Answers

First, you need to setup a system to monitor for file events from that folder. To get started, take a look at FindFirstChangeNotification().

You'll basically get a waitable handle from that.

Then, were it me, I'd have a thread that waited on that event. Each time the event triggers, the thread resumes, queries for the change details (what file), then perform the needed actions, and resume sleeping on that handle again.

You'll need some additional semaphore or something to use to interrupt this worker-thread and wake it so that you can tell it to quit. Simple to do: have your thread's main loop do a WaitForMultipleObjects - the "wake up semaphore" and the FindFirstChangeNotification handle. When you wake up, check which even notified you, then either process the file change or quit.

like image 156
Mordachai Avatar answered Dec 06 '25 05:12

Mordachai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!