Detecting real-time file directory changes
In this recipe, we will instantly detect when a new file is created, modified, or deleted. Similar to the popular file synchronization software Dropbox, we will be able to do interesting actions every time such an event occurs.
Getting ready
Install the fsnotify
package:
$ cabal install fsnotify
How to do it…
In a new file called Main.hs
, perform these steps:
- Import the relevant libraries:
{-# LANGUAGE OverloadedStrings #-} import Filesystem.Path.CurrentOS import System.FSNotify import Filesystem import Filesystem.Path (filename)
- Run the file watcher on the current directory:
main :: IO () main = do wd <- getWorkingDirectory print wd man <- startManager watchTree man wd (const True) doWork putStrLn "press return to stop" getLine putStrLn "watching stopped, press return to exit" stopManager man getLine return ()
- Handle each of the file change events. In this recipe, we just print out the action to...