Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 to stop"
    
      getLine
      putStrLn "watching stopped, press return to exit"
    
      stopManager man
      getLine
      return ()
  3. Handle each of the file change events. In this recipe, we just print out the action to the console:

    doWork :: Event...