Monday 30 May 2016

Haskell: renameDirectory: Rename a directory

System.Directory module provides renameDirectory function to rename a directory. Following is the signature of renameDirectory function.

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

Example
renameDirectory oldDir newDir

If the newDir already exists, it is atomically replaced by the oldDir. On Win32 platforms, renameDirectory fails if the newDir already exists.

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

Error
Description
HardwareFault
A physical I/O error has occurred.
InvalidArgument
Either operand is not a valid directory name.
isDoesNotExistError / NoSuchThing
The original directory does not exist, or there is no path to the target.
isPermissionError / PermissionDenied
The process has insufficient privileges to perform the operation.
ResourceExhausted
Insufficient resources are available to perform the operation.
UnsatisfiedConstraints
Implementation-dependent constraints are not satisfied.
UnsupportedOperation
The implementation does not support renaming in this situation.
InappropriateType
Either path 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"
    oldDir <- getLine

    putStrLn "Enter new directory name"
    newDir <- getLine

    renameDirectory oldDir newDir
    putStrLn "Directory renamed"

$ runghc DirectoryUtil.hs 
Enter directory name
/Users/harikrishna_gurram/dir1
Enter new directory name
/Users/harikrishna_gurram/dir2



Previous                                                 Next                                                 Home

No comments:

Post a Comment