Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 ()

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.

A physical I/O error has occurred.
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.
Insufficient resources are available to perform the operation.
Implementation-dependent constraints are not satisfied.
The implementation does not support renaming in this situation.
Either path refers to an existing non-directory object.

{- 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
Enter new directory name

Previous                                                 Next                                                 Home

This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Haskell: renameDirectory: Rename a directory


Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription