I have a list of file names (with full path) and I want to create a Dired buffer listing this set of files. How would I go about doing that?
How do I create a Dired buffer from a list of file names?
Without using external dependencies.
C-h f dired tells you the answer. Just pass to
dired, as the
DIRNAME argument, a list that has as its car the Dired buffer name you want (any string) and as cdr the list of file names you want listed in the buffer. Generally, you want to use absolute file names. For example:
(dired (list "My Dired Buffer Name*" ; The Dired buffer name "/usr/foo/file1.el" ; First file "/usr/bar/toto/some-file.c" ; Second file "/whatever/directory/" ; Third is a directory "/a/file/somewhere.zzz")) ; Fourth
Depending on your platform, names of any nonexistent files and directories you enter might be ignored (not listed), or might raise an error. The former happens if you use
ls-lisp.el (e.g. MS Windows). You can force the use of
ls-lisp on any platform, if you like (but
ls-lisp has fewer
ls options), by doing this:
(setq ls-lisp-use-insert-directory-program nil) (require 'ls-lisp)
If you use library Dired+ (
dired+.el) then you can interactively choose files and directories to list: just give
dired a non-positive prefix argument (i.e., <= 0). You are then prompted repeatedly for the directories and file names you want listed. You can use file-name wildcards (i.e.,
* for globbing), to include the matching files and directories. Use
C-g to end inputting.
In other words, instead of listing a single directory, the Dired buffer can list any number of directories and file names, which can even belong to different directory trees.
(A non-negative prefix arg (i.e., >= 0) prompts you for the
ls listing switches. So a zero prefix arg prompts you for both switches and files/dirs to list: first the
ls switches and then the files/dirs.)
Here's a command to do this when the list of files is in a buffer, with one file per line.
(defun dired-virtual-vanilla () (interactive) (dired (cons "*Dired*" (split-string (buffer-string) "\n" t))))