Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file list by extension?

Tags:

elisp

I want to get a list of all files with specific extension in a directory, and then pass it as an argument. How to do it in ELisp?

like image 889
Vivodo Avatar asked Feb 19 '23 22:02

Vivodo


2 Answers

No, you do not need any extra filtering. directory-files does everything you want, i.e., including the filtering to match the extension (e.g. ext):

(directory-files DIRECTORY nil "\\.ext$")

See C-h f directory-files. For the current directory, you can use:

(directory-files default-directory nil "\\.ext$")
like image 52
Drew Avatar answered Apr 29 '23 05:04

Drew


directory-files retrieves all the files in a directory as a list. remove-if-not removes list entries not matching a predicate.

(require 'cl)
(remove-if-not (lambda (filename) (eq "ext" (file-name-extension filename)))
               (directory-files dir) )

(I'd love to avoid the (require 'cl) there. http://emacswiki.org/emacs/ElispCookbook#toc38 has some hints.)

like image 30
tripleee Avatar answered Apr 29 '23 06:04

tripleee