0

This works, but the output file is in the same directory as the source file:

$ emacs ./notes.org --batch -f org-html-export-to-html --kill

How can I get the output in a different directory?

I tried what follows, with no success:

$ emacs ./notes.org --batch -f org-html-export-to-html  --eval '(write-file "~/dest/notes.html")' --kill
$ emacs ./notes.org --batch -f org-html-export-to-html > ~/dest/notes.html --kill
$ emacs ./notes.org --batch -f org-html-export-to-html >> ~/dest/notes.html --kill
1
  • 1
    org-html-export-to-html writes its output to ./notes.html: you don't get a choice. If you want to write to a different file, you will need to use a different function.
    – NickD
    Oct 18, 2023 at 17:01

1 Answer 1

1

You will need to use a different function if you want to control where the export output goes. org-html-export-to-html uses the name of the Org mode file and saves it in the same directory with the same name, except that the suffix is changed to .html from .org (except if EXPORT_FILE_NAME is used - see below).

Here is a function that you can use to write the output of the exporter to a different file:

(defun my/org-html-export-to-file (file)
  (let ((output (org-export-as 'html)))
    (with-temp-buffer
      (insert output)
      (write-file file))))

The function stores the output of the exporter as a string and then creates a temporary buffer where it inserts the string - it then saves the buffer to the file given as argument to the function.

Assuming that you have saved the function to a file, say ~/src/emacs/lisp/org-html-to-file.el, then the command line invocation becomes:

emacs --batch ./notes.org \
       --load "~/src/emacs/lisp/org-html-to-file.el" \
       --eval '(my/org-html-export-to-file "~/dest/notes.html")'

If the destination directory does not exist already, you will be asked whether to create it, which probably defeats the purpose, so you should probably create it in advance.


Alternatively, you can add a keyword line like this to your file:

#+EXPORT_FILE_NAME: ~/dest/notes.html

See Export settings.

The command line is simpler, basically what you wrote in your question, but without the (futile) attempt to eval a different destination or redirect the output:

emacs --batch /tmp/foo.org -f org-html-export-to-html
1
  • 1
    Yes, or you can change the function to take two arguments: the backend and a file; and change the invocation appropriately. As for multiple EXPORT_FILE_NAME lines, you can just write #+EXPORT_FILE_NAME: ~/dest/notes and let the backend add the suffix.
    – NickD
    Oct 19, 2023 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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