Search code examples
emacselisp

Is there a function included with Emacs to recursively walk the directory structure and call a function?


I would like to recursively walk down the directory structure from a particular point and call copyright-update-directory at each level.

Is there a function included with Emacs 24 that would help with this? For example something like:

(recursive-directory-walk "~/src/foo" '(copyright-update-directory))

If no such function exists, some pointers on getting started implementing this (or a working implementation) would be great.


Solution

  • Not sure if exists, but not very hard to write your own.

    (defun folder-dirs (folder)
      (delete-if-not 'file-directory-p
        (mapcar (lambda(arg) (file-name-as-directory (concat (file-name-as-directory folder) arg)))
          (delete-if (lambda (arg) (or (string= ".." arg) (string= "." arg)))
            (directory-files folder)))))
    
    (defun recursively-run-on-every-dir (fn folder)
    "FN - function, taking one argument; 
    FOLDER - initial forder"
      (funcall fn folder)
      (mapc (lambda(arg) (recursively-run-on-every-dir fn arg))
        (folder-dirs folder))
      nil)
    
    ;; use your function instead of print
    (recursively-run-on-every-dir 'print "/your/initial/path/")