Script latest-files.scm

From CCP4 wiki
;; find the most recently created file from the given glob and dir
;;
;; return #f on no-such-file
;; 
(define most-recently-created-file
  (lambda (glob-str dir)

    (define add-dir-prefix
      (lambda (file)
	(if (string? file)
	    (append-dir-file dir file)
	    file)))

    (let ((files (glob glob-str dir)))

      (format #t "most-recently-created-file sees files: ~s by globbing ~s in ~s ~%" files
	      glob-str dir)

      (let loop ((files files)
                 (latest-file #f)
                 (latest-mtime 0))

        (cond
         ((null? files) (add-dir-prefix latest-file))
         (else 
          (let ((this-mtime (stat:mtime (stat (add-dir-prefix (car files))))))
            (if (> this-mtime latest-mtime)
                (loop (cdr files)
                      (car files)
                      this-mtime)
                (loop (cdr files)
                      latest-file
                      latest-mtime)))))))))

;; load latest mtz and pdb files in this directory.
;; 
(define load-latest-files
  (lambda ()

    ;; If pdb-file and mtz-file are strings, then try to load them
    ;; into coot.
    (define load-latest-pdb-mtz-files
      (lambda (pdb-file-name mtz-file-name)
	
	(if (string? pdb-file-name)
	    (if (file-exists? pdb-file-name)
		(handle-read-draw-molecule-with-recentre pdb-file-name 0)))
	
	(if (string? mtz-file-name)
	    (if (file-exists? mtz-file-name)
		(auto-read-make-and-draw-maps mtz-file-name)))))
    
    ; main line
    (load-latest-pdb-mtz-files
     (most-recently-created-file "*.pdb" ".")
     (most-recently-created-file "*.mtz" "."))))