Monday, 30 May 2016

Haskell: createDirectory: Create a directory

System.Directory module provides createDirectory function to create a directory. Following is the signature of createDirectory function.

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

Observe the signature, it takes full path of firectory as an argument and creates the directory with given name. As per the documentation, you may get following errors while creating a directory.

Error
Description
isPermissionError
Occur when the process don’t have permission to create directory.
isAlreadyExistsError
Occur when directory already exists.
HardwareFault
A physical I/O error has occurred.
InvalidArgument
FilePath is not a valid directory name.
NoSuchThing
There is no path to the directory.
ResourceExhausted
Occur when resources such as memory, file descriptors etc., are not available to create directory.
InappropriateType
Occur when the FilePath 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 name"
    direcotryName <- getLine

    createDirectory direcotryName
    putStrLn "Directory created"

$ runghc DirectoryUtil.hs 
Enter directory name
/Users/harikrishna_gurram/my_evenings
Directory created
$
$ du -hc /Users/harikrishna_gurram/my_evenings
  0B /Users/harikrishna_gurram/my_evenings
  0B total
createDirectory function fail, if directory is already exist.

$ runghc DirectoryUtil.hs 
Enter directory name
/Users/harikrishna_gurram/my_evenings
DirectoryUtil.hs: /Users/harikrishna_gurram/my_evenings: createDirectory: already exists (File exists)


Previous                                                 Next                                                 Home

No comments:

Post a Comment