1

How can I filter directory contents to return the highest numeric named directory? For example, suppose (directory-files "/some/dir") returns:

("." ".." "4.03.0" "4.02.9" "archives" "config" "log")

How can I extract the string "4.03.0"?

I tried using seq-remove with a regexp based predicate, but could not get it to work. Suggestions and insights appreciated. Thanks.

1

directory-files accepts match-regexp argument:

(directory-files "my/path/to/dir" nil "^[0-9][0-9.]*$")

should return ("4.03.0" "4.02.9") and you extract the first element of the list using functions first or car.

If you really want to use seq-remove, you can do that too:

(seq-remove
 (lambda (s)
   (not (string-match "^[0-9][0-9.]*$" s)))
 '("." ".." "4.03.0" "4.02.9" "archives" "config" "log"))
==> ("4.03.0" "4.02.9")

Note that ^[0-9.]$ matches "." and "..".

  • Thanks for the helpful answer. Does directory-files ensure descending ordering? If not, is there a way to extract the target string independent of directory list order (i.e. assuming no particular order)? – Ari May 19 '17 at 17:18
  • @Ari: You are welcome. Order is determined by the 4th argument to directory-files. If that is not your question, you should close this one and ask another one. – sds May 19 '17 at 17:20
  • 2
    @Ari: emacs is self-documenting, so you can find out about a function by asking emacs about it: C-h f directory-files e.g. will tell you what the function does and what arguments it takes. There is a similar facility for variables. Just do C-h ? to get more information. – NickD May 19 '17 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.