Monday, 30 May 2016

Haskell: removeDirectory: Remove an existing directory

System.Directory module provides removeDirectory function, which takes a directory full path as an argument and removes it. Following is the signature of the removeDirectory function.

Prelude System.Directory> :t removeDirectory
removeDirectory :: FilePath -> IO ()

As per the documentation, you may get following erros while working with removeDirectory function.

Error
Description
HardwareFault
A physical I/O error has occurred.
InvalidArgument
FilePath is not a valid directory name.
isDoesNotExistError/ NoSuchThing
The directory does not exist.
isPermissionError / PermissionDenied
The process has insufficient privileges to perform the operation.
UnsatisfiedConstraints
Implementation-dependent constraints are not satisfied like .
UnsupportedOperation
The implementation does not support removal in this situation.
InappropriateType
The operand refers to an existing non-directory object.


DirectoryUtil.hs
{- Take directory name as input and create a directory -}
import System.IO
import System.Directory

main = do
    putStrLn "Enter directory full path to remove"
    direcotryName <- getLine

    removeDirectory direcotryName
    putStrLn "Directory removed"

$ runghc DirectoryUtil.hs 
Enter directory full path to remove
/Users/harikrishna_gurram/today/evenings/1
Directory removed


removeDirectory fail with an error, if the directory is not empty.
$ runghc DirectoryUtil.hs 
Enter directory full path to remove
/Users/harikrishna_gurram/today
DirectoryUtil.hs: /Users/harikrishna_gurram/today: removeDirectory: unsatisfied constraints (Directory not empty)



Previous                                                 Next                                                 Home

No comments:

Post a Comment