org-element-parse-buffer
and org-element-map
are your friends.
You get the doc strings of these two functions (and of every other function) by C-h f.
The following function org-headlines-to-dir-tree
parses the org buffer via org-element-parse-buffer
and creates the directories corresponding to the headlines via org-element-map
.
This is pure Elisp. No shell commands are required.
(defun org-headlines-to-dir-tree (dir &optional data)
"Transform org headlines in DATA to directory tree below DIR.
DATA defaults to the headline structure of the current org buffer."
(interactive "DTarget directory:")
(unless data
(unless (derived-mode-p 'org-mode)
(user-error "%S is not an org buffer" (current-buffer)))
(setq data (org-element-parse-buffer 'headline)))
(unless (file-directory-p dir)
(user-error "%S is not a directory" dir))
(let ((default-directory (expand-file-name dir)))
(org-element-map
data
'headline
(lambda (el)
(let ((title (org-element-property :title el))
(contents (org-element-contents el)))
(when (y-or-n-p (format "Create directory %S?" (expand-file-name title)))
(mkdir title)
(when contents
(org-headlines-to-dir-tree title contents))))) ;; Recursion.
nil ;; info
nil ;; first match
'headline ;; no-recursion (We do the recursion ourselves with additional directory changes.)
)))
You can paste that stuff into your *scratch*
buffer and evaluate it by placing point in to the function body and typing C-M-x.