2015-06-17 17 views

Trả lời

0

Sử dụng f:

(defun aj-fetch-latest (path) 
    (let ((e (f-entries path))) 
    (car (sort e (lambda (a b) 
        (not (time-less-p (aj-mtime a) 
            (aj-mtime b)))))))) 
(defun aj-mtime (f) (let ((attrs (file-attributes f))) (nth 5 attrs))) 
6

Sử dụng API built-in có thể đạt được cũng như:

(defun latest-file (path) 
    "Get latest file (including directory) in PATH." 
    (car (directory-files path 'full nil #'file-newer-than-file-p))) 

(latest-file "~/.emacs.d") ;; => "/Users/xcy/.emacs.d/var" 

Nếu bạn cũng cần tập tin theo thư mục, sử dụng directory-files-recursively hơn directory-files. Nếu bạn muốn loại trừ các thư mục, trước tiên hãy lọc danh sách tệp/thư mục bằng cách sử dụng file-directory-p.

+0

lưu ý rằng thuật toán này yêu cầu truy cập đĩa 'N * log (N) *, trong đó' N' là số tệp trong thư mục. – sds

+0

@sds Tôi đã cập nhật câu trả lời của mình bằng cách sử dụng 'thư mục-tệp' trực tiếp thay vì tự sắp xếp. – xuchunyang

+1

'file-newer-than-file-p' truy cập đĩa hai lần. – sds

Các vấn đề liên quan