proposed addition to System.Directory

Simon Marlow simonmar at microsoft.com
Wed Jan 12 08:07:51 EST 2005


On 12 January 2005 10:31, Ross Paterson wrote:

> I find the following generalization of copyFile useful.  Perhaps it
> could be added to System.Directory.
> 
> {- |@'copyContents' old new@ copies the contents of /old/ to /new/,
> creating /new/ if it does not already exist.
> However the parent directory of /new/ must already exist.
> If /old/ is a plain file, 'copyContents' is equivalent to 'copyFile'.
> If /old/ is a directory, its contents are recursively copied into
> /new/, which, if it already exists, must be a directory.
> -}
> copyContents :: FilePath -> FilePath -> IO ()
> copyContents fromFPath toFPath = do
>   isFile <- doesFileExist fromFPath
>   if isFile then
>     copyFile fromFPath toFPath
>    else do
>     contents <- getDirectoryContents fromFPath
>     createDirectoryIfMissing False toFPath
>     sequence_ [copyContents (fromFPath `joinFileName` f)
> 				(toFPath `joinFileName` f) |
> 		f <- contents, f /= "." && f /= ".."]

The behaviour is different from 'cp -r', so at least to me it feels a
bit unusual.  However, it's more consistent with copyFile, I suppose.
An example or two in the documentation wouldn't go amiss.

Cheers,
	Simon


More information about the Libraries mailing list