2009-08-24

emacs lisp function to copy dir and all sub dir?

is there a emacs lisp command to copy a whole dir? like unix's cp -R?

i started to write one:

(defun copy-dir (sourcedir destdir)
"Copy all files from SOURCEDIR to DESTDIR.

The input dir should not end in a slash.
Example usage:
(copy-dir
\"/Users/xah/web/p/um\"
\"/Users/xah/web/diklo/xx\")

Note: no consideration is taken about links, alias, or file perms."
(mapc
(lambda (x)
(let ()
(when (and (not (string-equal x ".")) (not (string-equal x "..")))
(copy-file
(concat sourcedir "/" x) destdir) ) ) )
(directory-files sourcedir) ) )

but it is hackish because of the checking with “.” and “..”,
but also realized this doesn't do sub dirs. In order to do subdir, it'll might end up with few hours more...

so, am wondering if there's a pre-made solutions?

Thanks.

Xah
∑ http://xahlee.org/



On Aug 26, 5:32 am, Michael Heerdegen wrote:
> Have a look at the doc of `dired-recursive-copies'.

sorry all, but a bit clarification.

i needed a copy-dir function for elisp coding, not as user function in dired.

i haven't thought about looking into how dired-do-copy implement it, good thought. thanks.

looking into it, it seems a bit complex:

(defun dired-do-copy (&optional arg)
"Copy all marked (or next ARG) files, or copy the current file.
This normally preserves the last-modified date when copying.
When operating on just the current file, you specify the new name.
When operating on multiple or marked files, you specify a directory,
and new copies of these files are made in that directory
with the same names that the files currently have. The default
suggested for the target directory depends on the value of
`dired-dwim-target', which see.

This command copies symbolic links by creating new ones,
like `cp -d'."
(interactive "P")
(let ((dired-recursive-copies dired-recursive-copies))
(dired-do-create-files 'copy (function dired-copy-file)
"Copy"
arg dired-keep-marker-copy
nil dired-copy-how-to-fn)))

is this the only way?

i think most modern scripting lang has a copy dir function... that'd be good in elisp.

Xah

------------------------------

for solution, see:

• Emacs Lisp Suggestion: A Function For Copying and Deleting Whole Dir
http://xahlee.org/emacs/modernization_elsip_copy_dir.html

No comments:

Post a Comment