Copy Folder

Function Syntax (LM:copyfolder <src> <des> <ovr>)
Current Version 1.1
Donate
Arguments
Symbol Type Description
src String Directory to be copied
des String Destination to which source directory contents will be copied
ovr Boolean Boolean value to determine whether existing files & folders are overwritten
Returns
Type Description
Boolean T if source folder content is copied is successfully, else nil.

Function Description

This function utilises the CopyFolder method of the FileSystemObject to recursively copy a directory structure and all files contained therein to a destination directory.

The destination directory will be created if non-existent, and the user may specify whether existing files & folders are overwritten when the directory is copied.

Please note: if an error occurs during copy operation, no attempt is made to roll back files and folders copied before the error occurred.

Select all
;; Copy Folder  -  Lee Mac
;; Uses the CopyFolder method of the FileSystemObject (FSO) to recursively
;; copy a folder from one location to another.
;; src - [str] Directory to be copied
;; des - [str] Destination directory
;; ovr - [bol] T=overwrite existing files, nil=do not overwrite
;; Returns T if copy was successful, else nil

(defun LM:copyfolder ( src des ovr / fso rtn )
    (if (setq fso (vlax-create-object "scripting.filesystemobject"))
        (progn
            (setq rtn
                (not
                    (or (zerop (vlax-invoke fso 'folderexists src))
                        (vl-catch-all-error-p
                            (vl-catch-all-apply 'vlax-invoke
                                (list fso 'copyfolder src des (if ovr :vlax-true :vlax-false))
                            )
                        )
                    )
                )
            )
            (vlax-release-object fso)
            rtn
        )
    )
)
(vl-load-com)

Example Function Call

(LM:copyfolder "C:\\sourcefolder" "C:\\destinationfolder" nil)

Alternative Version

For academia, I have written an alternative version of the above function without using the FileSystemObject.

The alternative function provided below uses the vl-file-copy & vl-mkdir Visual LISP functions to perform in a similar way to the above function, however without the 'overwrite' flag.

The function will return T if all files & folders have been copied successfully; however, due to the inherent behaviour of the vl-file-copy function, this function will not overwrite existing files & folders and will hence return nil if one or more files already exists.

Select all
;; Copy Folder  -  Lee Mac
;; Recursively copies the contents of a folder and the contents of any
;; subfolders to a destination directory; will not overwrite existing files.
;; src - [str] Directory to be copied
;; des - [str] Destination directory
;; Returns T if all files were copied successfully, else nil

(defun LM:copyfolder ( src des )
    (vl-mkdir des)
    (apply 'and
        (append
            (mapcar
                (function
                    (lambda ( file )
                        (vl-file-copy (strcat src "\\" file) (strcat des "\\" file))
                    )
                )    
                (vl-directory-files src nil 1)
            )
            (mapcar
                (function
                    (lambda ( dir )
                        (LM:copyfolder (strcat src "\\" dir) (strcat des "\\" dir))
                    )
                )
                (vl-remove "." (vl-remove ".." (vl-directory-files src nil -1)))
            )
        )
    )
)

Example Function Call

(LM:copyfolder "C:\\sourcefolder" "C:\\destinationfolder")

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010