The idea for this project came from a script utilized in a previous job. In a shared directory, folders that were deleted would automatically be recreated and certain files would be merged from several folders into one. It was an interesting idea, so I decided to recreate that behavior with my own implementation.
The core of this script is the Watchdog module which essentially watches a specified directory for changes, then executes certain actions.
- When a folder is deleted or renamed, the original will be recreated.
- When a file is created, it will be moved to another folder if specified.
I use a Microsoft SQL Server database to store a list of Folders that I want to always be in the specified location. Whenever a creation/modification/deletion event occurs in the root location, the maintain() function is called and the SQL table is queried and missing folders will be recreated.
There is another column in the table that indicates a Merge location. If I want files from certain folders to be put in a single folder, it is entered here. Then, every time a file is added in the 'source' folders, it will trigger the merge() function and move that file to the proper folder.