Assume a structure like this:

What I want is to convert all the text-files of a folder to one org mode file per folder. The org headings should contain the name of the orginating text files and below should be the content of the orginating text files. What would be the exact commands to reach that goal?

Find in combination with Pandoc should be a solution. I found the releated thread about markdown-file conversion below, but the altering is too difficult for me.

How to migrate Markdown files to Emacs org mode format

  • 2
    How does pandoc come in the picture here? You have simple plain text files without any markup language to begin with, right? Pandoc is useful where you want to convert from one markup to another; like markdown -> org, html -> markdown, etc. Or do your txt files have some sort of markup? If not, you can simply merge the files, add * FILENAME and rename *.txt to *.org. – Kaushal Modi May 12 '16 at 22:21
  • You are probably right about pandoc but then the question would be how do I do the stuff you mentioned after "you can simply"? I don't think I can find this out by myself in a reasonable time. – Jens Lange May 14 '16 at 1:38
  • Is this a one-time conversion? Will you be adding or updating new text files in future that will need to be synchronized with the org documents? Will you need to recreate the original files at a future date? – Melioratus Sep 19 '16 at 16:45
  • Yes, one time - it's for my old files since new stuff is done in org mode properly categorized and hierarchically structured. No new files. No recreation. No links. The customer folders contain also other file formats, they will remain, but the text files will be deleted after the conversion to an org file. – Jens Lange Oct 20 '16 at 3:50

You can use the following commands to create such kind of org files for a given directory:

(defun my-dir-to-org (dir org-file)
  "Create a file ORG-FILE which has all txt files in DIR as linked headlines
and the contents of the files below the headlines."
  (interactive "DDirectory to convert: \nFFilename: ")
  (let ((files (directory-files
                dir t ".*\\.txt\\'")))                     
    (with-temp-file org-file
      (dolist (file files)
        (insert (concat "* " "[[" file "][" (file-name-nondirectory file) "]]\n\n"))
        (insert-file-contents file)
        (goto-char (point-max))
        (insert "\n")))))

(defun my-mass-conversion (source-dir target-dir)
  "Create one org file per directory of SOURCE-DIR inside TARGET-DIR."
  (interactive "DDirectory to convert: \nDTarget Directory:")
  (let ((dirs-full
         (remove-if-not #'file-directory-p
                         source-dir t

    (mapc (lambda (dir)
            (my-dir-to-org dir
                           (concat target-dir
                                   (file-name-base dir) ".org")))
  • This is quite impressive. It works nearly as needed. Currently I got two things, first the my-dir-to-org function creates links to the original files, it looks great, but fact is that after a text-to-org processing I would delete the original files, so all links would be dead. The second problem is that it doesn't "drill down". If I got one level in the source dir it works as intended but if files are two levels down (or more) they won't be reached/included. – Jens Lange Oct 20 '16 at 3:42
  • I will mark your answer as accepted answer. Even though it still has the problems mentioned above it is still good enough and the only lisp based answer. My dos batch is only a workaround. – Jens Lange Oct 21 '16 at 2:03

Well I have a "good-enough-until-I-get-something-better" solution. The solution is a batch file, that when executed in a folder puts the foldername at the first level of an org-file and the name of the contained text files in the second level. Under every name of a text file is the content of it. Batch is mostly written by somebody else, for some other merging purpose, but I managed to alter it that it works a bit the way I need it.

Any improvement/fix/better solution still more than welcomed.

for %%* in (.) do (echo * %%~nx* >> output.org)
for /r %%i in (*.txt , *.cfg) do (
if not %%~nxi == output.org (
echo ** %%~nxi >> output.org
type "%%i" >> output.org
echo. >> output.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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