proposed addition to System.Directory

Ross Paterson ross at soi.city.ac.uk
Wed Jan 12 05:31:09 EST 2005


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 /= ".."]


More information about the Libraries mailing list