10

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.

  • Please clarify. Are the strings in your list a) the names of the files you wish to create, or b) the content of the file (the thing for which you have an absolute path)? – Dan Oct 24 '14 at 19:05
  • @Dan A list of filenames with absolute paths. Just the filenames. – Tu Do Oct 24 '14 at 19:08
  • 1
    If I understand you correctly, you can just use (find-file your-directory-path) to get the relevant dired buffer (e.g., (find-file "/tmp/") will open up your tmp directory in a dired buffer). – Dan Oct 24 '14 at 19:42
  • 1
    I've edited your question based on what you said in these comments. Feel free to rollback if I got it wrong. – Malabarba Oct 24 '14 at 21:41
  • 3
    If you want to ask about file objects in elisp that's an entirely different question, with a very short answer: there aren't any. :-) – Malabarba Oct 24 '14 at 21:43
20

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.)

  • Well I want to do it in Elisp, not using command. I just want a simple code snippet that turn a file name (with full path) into an entry in Dired buffer, similar to how I use (insert ...) to insert text into a buffer. – Tu Do Oct 25 '14 at 5:38
  • 1
    @tudo: dired is an elisp function. Just evaluate this: (dired '("my-list" "/home/rekado/dev/script.scm" "/home/rekado/another-file")) and you get a dired buffer containing these files as @Drew wrote. – rekado Oct 25 '14 at 6:47
  • @rekado .Thanks. Now I reread the answer correctly it's like what you said. I just want to see an Elisp code snippet like yours, and I thought that Drew gave me a user command answer when I glimpsed it. – Tu Do Oct 25 '14 at 7:05
  • @Drew Please add rekado's example. Thanks for your answer. – Tu Do Oct 25 '14 at 7:10
  • Initially I didn't think it would be this simple, when I see filesets. I thought we must make Dired recognize proper file objects for it to work properly. – Tu Do Oct 25 '14 at 7:21
2

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))))

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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