Monday, 30 May 2016

Haskell: getDirectoryContents: List all entries in the directory including special entries ., ..

System.Directory function provides getDirectoryContents function which return a list that contains all the entries in the directory (including ., ..). Following is the signature of getDirectoryContents function.

Prelude System.Directory> :t getDirectoryContents
getDirectoryContents :: FilePath -> IO [FilePath]

As per the documentation, you may get following error while working with getDirectoryContents function.

Error
Description
HardwareFault
A physical I/O error has occurred.
InvalidArgument
The operand is not a valid directory name.
isDoesNotExistError / NoSuchThing
The directory does not exist.
isPermissionError / PermissionDenied
The process has insufficient privileges to perform the operation.
ResourceExhausted
Insufficient resources are available to perform the operation.
InappropriateType
The path refers to an existing non-directory object.


DirectoryUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter directory name"
    dir <- getLine

    fileNames <- getDirectoryContents dir
    print fileNames

$ runghc DirectoryUtil.hs 
Enter directory name
/Users
[".","..",".localized","harikrishna_gurram","Shared"]



Previous                                                 Next                                                 Home

No comments:

Post a Comment