2

I have a folder with 3000+ txt files (made in nvALT originally) that I would like to import into an org-mode file.

Preferrably I would like each file name to become a header in the org-file.

The order of the files/headers are not really important, but if possible I would like to add them based on creation date.

Ps. I know about deft, but it really slows down when I have so many files, and I cannot use all the powers of org there.

3
  • 4
    Feels like something that can be done in a couple of lines with bash/zsh or python or whatever
    – Swedgin
    Oct 5, 2021 at 9:27
  • @Swedgin Yes! Surely. I am a noob in the terminal however… Have tried to read up on cat and similar, but cannot really get my head around it.
    – trmdttr
    Oct 5, 2021 at 9:34
  • 2
    Check out stackoverflow.com/questions/23408782/… for looping over files and get their name aswell. Check out the '>>' operator to append to files (my first hit: serverfault.com/questions/196734/…)
    – Swedgin
    Oct 5, 2021 at 9:59

1 Answer 1

5

Here's an elisp function that prompts you for a directory name, and then inserts all the txt files in that directory into the current buffer. It sorts by last modified time too. If you want to change the file names that it matches, you can modify the string "txt" to suit your needs. Or add it as an additional interactive argument.

(defun orgify-directory (DIR)
  (interactive "DDirectory: ")
  (mapc (lambda (elt)
          (insert (concat "\n* " ;; insert headline
                          (file-name-nondirectory (car elt)) 
                          "\n\n")) 
          (insert-file-contents (car elt)) ;; insert file contents
          (goto-char (point-max))) ;; jump to end of buffer
        (seq-sort-by ;; sort by last modified time
         (lambda (elt) (time-to-seconds (nth 6 elt))) #'< 
         (directory-files-and-attributes DIR t "txt$"))))
4
  • Hey! Thank you! It almost works. It puts in all the filenames as headers first (including full path) and then the contents afterwards. Could it be made without paths and with content in the right place? (Added a test result below, sadly without line breaks)
    – trmdttr
    Oct 6, 2021 at 7:08
  • * /Users/xjtorm/Dropbox/orgify/foo.txt * /Users/xjtorm/Dropbox/orgify/bar.txt This is the bar file. This is the foo file.
    – trmdttr
    Oct 6, 2021 at 7:08
  • @trmdttr Try the new version...
    – Tyler
    Oct 6, 2021 at 13:36
  • That's amazing. Works perfectly. 🙏
    – trmdttr
    Oct 6, 2021 at 16:12

Your Answer

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

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