File Watcher: Monitor Changes to Files and Folders
File Watcher is a standalone, open source, C# application that I have developed to serve a purpose that I needed for my data files. In a nutshell, File Watcher allows me to monitor several paths on my Windows server for any changes to the files or folders within those paths. Once changes are detected, one of several functions can then be performed. These functions include: sending a notification via an API request, performing an action (copy, move, delete), or running a command – such as an executable.
The post below will outline the idea and development of File Watcher.
The idea of File Watcher
It has been a few years since I have written about my backup strategy for my data files. For all intents and purposes, I have been using the same strategy for years. The number of files has grown over the years since I talked about my backup strategy, but my strategy hasn’t changed.
As with everything I do in computers, I like to revisit things, and while reviewing how my files are stored I came to realize that I don’t monitor the files in two ways:
- If files are accidently, or malicously changed. The files I store are mainly family photos and videos, which I never want to change, os if one does change, how will I know unless I open each file.
- If files are changed in a small way, how can I detect if even a bit in the files has changed. There are file systems that can detect and correct this, but since I am on Windows, my file system doesn’t have this capability.
Some may think that I have become paranoid about my files, but when it comes to family memories – I can’t replace those files if they become unreadable, or drastically altered. Besides, I figured there may be some way I can automate monitoring and validating my files without having to manually do so. In addition, when the files are monitored, I would like to be notified if something is detected that I would need to take action on.
In the post where I talked about backing up my files automatically, I mentioned that I had a staging service that I had developed that monitored a specific staging folder for files, and then moved those files to their actual folders. This allowed me to make the staging folder a read/write share on my desktop, and the share containing the actual folder as read-only to help avoid accidental changes. I decided to use what I learned from this staging service to develop a new, more feature-rich application that will do more than just move files from one folder to another.
The features of File Watcher
After developing File Watcher, I included the following functions:
Completely open source
File Watcher is open source, and can be found (and downloaded) from the Github File Watcher repo.
The application does not require it to be installed. You can just unzip the file from Github, create a configuration file, and then run the executable. You can, as I do now, run it as a Windows service to ensure it is always running.
I have created documention for File Watcher in the Github repo to help create the configuration file and run the application.
Configuration via XML file
While I could have used JSON, or another markup language, I settled on XML as my configuration file type. The reason is because I find XML is easier to read and understand. I did leave open the possibilty of supporting multiple configuration markup languages in the future, though.
Monitor multiple paths
File Watcher can monitor changes to several different paths on both an internal and external hard drive. I don’t recommend using it to monitor a network share, such as a connection to a NAS.
If a change is detected in a path, File Watcher can send an API request to an endpoint for notifications. I currently use File Watcher to send a request to my Gotify server if a change is detected. To avoid overloading my server, I implemented a hard-coded minimum of 30 seconds between requests (the messages are queued), but this time can be set higher in the configuration file.
Perform a specific action
Three actions – copy, move, or delete – can be performed if a change is detected. This allows me to replace my staging server, as File Watcher can copy files from my staging folder to the actual folder. I simply have File Watcher monitor the path of my staging folder for changes.
Execute a command
In addition to monitoring my files, I also developed an application that generates hashes for all files in a folder and stores the hashes in a text file in that folder. I then run monthly compares that will validate the current file hash matches the stored hash value to see if the file has changed. File Watcher will run the same hash-generating application when a new file is created, so I automatically get a new hash for the file for comparison the next time I run the monthly comparison.
Exclude files and folders
File Watcher does have the ability to ignore changes to files and folders based on file or folder names, attributes, and path values. If a file or folder change matches any item on the exclusion lists, then the change isn’t processed.
Process files only specific changes
Some folders I didn’t need to process changes for all types of file changes. For example, with my staging folder I only needed to process files that were created – not changed or deleted. With File Watcher, I provided the ability to only trigger the functions when a specific change is detected.
File Watcher is a new application that I recently developed to fulfill a need that I had with my current data files. It was built to monitor specific paths on my server, and notify me of any changes that have been made to the files. As with all my developed projects, File Watcher will continue to be developed with new features that I may need or someone has requested.
This is an application that I will be using for many years to come.