Lisp to copy file if modified

Lisp to copy file if modified

sjdunn-2019
Explorer Explorer
557 Views
3 Replies
Message 1 of 4

Lisp to copy file if modified

sjdunn-2019
Explorer
Explorer

Need someone better at lisp than me, trying to use lisp to copy a file from one location to another if date modified of source file is newer than destination file. Put some pieces together but doesn't quite work as expected. I think systime looks at date created for file vs date modified? Any suggestions?

 

(defun c:copyIfModified (/ sourceFile destFile sourceDate destDate)
  (setq sourceFile "N:\\CAD\\Details\\sample.dwt")
  (setq destFile "C:\\CAD\\Details\\sample.dwt")
 
  ;; Get the last modified date of the source file 
(setq sourceDate (vl-file-systime sourceFile))
 
  ;; Check if the destination file exists
(if (findfile destFile)
    (progn
;; Get the last modified date of the destination file
(setq destDate (vl-file-systime destFile))
      ;; Compare the source and destination file dates
(if (/= sourceDate destDate)
        (progn
;; If the dates are different, delete original and copy the source file to the destination
(vl-file-delete destFile)
(vl-file-copy sourceFile destFile)
          (princ "\nFile copied1.")
        )
        (princ "\nNo changes detected.")
      )
    )
    (progn
;; If the destination file doesn't exist, copy the source file to the destination
      (vl-file-copy sourceFile destFile)
      (princ "\nFile copied2.")
    )
  )
  (princ)
)
0 Likes
558 Views
3 Replies
Replies (3)
Message 2 of 4

paullimapa
Mentor
Mentor

@sjdunn-2019 Looks like your code just uses a not equal symbol which does not distinguish older vs newer:

 

(/= sourceDate destDate)

 

According to the online manual, vl-file-systime does return last modification date.

The return vl-file-systime value is in a list with 7 or 8 items:

  • year
  • month
  • day of week
  • day of month
  • hours
  • minutes
  • seconds

So what you can do is compare each item <<depending on length of return list (nth 0 through nth 6) or (nth 0 through nth 7)> to see if the source file is newer than the destination.

Once you get a match where the source file has an item that is greater than the destination then you can stop the comparison and continue with the copy.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 4

ronjonp
Mentor
Mentor

@sjdunn-2019 

Use XCOPY or ROBOCOPY. Here's some lisp code I put together a while back.

0 Likes
Message 4 of 4

sjdunn-2019
Explorer
Explorer

It took me a little while to get back at this plus doing a little testing. I ended up using the startapp option to run a batch file and used robocopy from within the batch file to copy modified files. The batch file is run when AutoCAD starts and this way I can modify the batch file as needed for additional files to update. Trying to keep it simple, thanks for the suggestions!

0 Likes