2

Moving a file seems easy, but is there a simple way to copy a file in a given path to another?

villasv
  • 6,304
  • 2
  • 44
  • 78

2 Answers2

4

The uiop package has such a function:

(uiop:copy-file source-path target-path)

It's part of ASDF, so it might be immediately available on some Common Lisp implementations.

villasv
  • 6,304
  • 2
  • 44
  • 78
2

While the sane answeer is to use the things ASDF ships, you can write this. Caution the code below has not been very carefully tested (but I use it for copying binaries around):

(defun copy-file (from to)
  ;; I'm sure there are now portability packages to do this but I do
  ;; not want to rely on them.  This is a naive implementation which
  ;; is not terrible.
  (with-open-file (out to :direction ':output
                       :if-exists ':supersede
                       :element-type '(unsigned-byte 8))
    (with-open-file (in from :direction ':input
                        :element-type '(unsigned-byte 8))
      (loop with buffer = (make-array 4096 :element-type '(unsigned-byte 8))
            for pos = (read-sequence buffer in)
            while (> pos 0)
            do (write-sequence buffer out :end pos)
            finally (return (values from to))))))