How to print a function output to a new file?

43 views Asked by At

I'm simply trying to export the filenames of marked files from a Dired buffer to a new text file.

Looking at the manual, I found the function (dired-get-marked-files).

In a Dired buffer, after marking three files for test purposes, evaluating:
(dired-get-marked-files 'verbatim nil nil nil t)
prints:
("vid_1.mp4" "vid_2.mp4" "vid_3.mp4").

So far so good, but my lack of elisp knowledge put me at a stop now. Basically I would like to have those three filenames on each line in a new text file fileslist.txt in the same directory like this:

file 'vid_1.mp4'
file 'vid_2.mp4'
file 'vid_3.mp4'

I tried something like below and calling it from the Dired buffer but it doesn't work.

(defun my/test-dired ()
  "fileslist.txt creation."
  (interactive)
  (with-temp-file "fileslist.txt" (dired-get-marked-files 'verbatim nil nil nil t)))
1

There are 1 answers

5
Drew On BEST ANSWER

Try this:

(defun write-marked-file-names-to-file ()
  "Write names of marked files to file `fileslist.txt'."
  (interactive)
  (unless (derived-mode-p 'dired-mode)
    (error "You must be in Dired or a mode derived from it to use this command"))
  (let ((files  (dired-get-marked-files 'verbatim nil nil nil t)))
    (with-temp-file "fileslist.txt"
      (dolist (file  files)
        (princ (concat "file '" file "'\n") (current-buffer))))))