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:

  1. Import the relevant libraries:
    {-# LANGUAGE OverloadedStrings #-}
    import Filesystem.Path.CurrentOS
    import System.FSNotify
    import Filesystem
    import Filesystem.Path (filename)
  2. 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 ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.