help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where to start with recursive file operations


From: Oliver Scholz
Subject: Re: Where to start with recursive file operations
Date: Sat, 06 Nov 2004 07:47:43 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3.50 (gnu/linux)

Friedrich Dominicus <just-for-news-frido@q-software-solutions.de> writes:

> Kevin Rodgers <ihs_4664@yahoo.com> writes:
>
>> Andrei Stebkov wrote:
>>> I would like to write a function that finds files recursively
>>> starting from the current directory, opens files based on given
>>> extension and does some operations on them.
[...]

>> I would write a small shell script that uses the find command to
>> generate the file names, and use emacs' command line options to do the
>> processing:
[shell script]

> Well that would be one way. I would prefer doing it all in Emacs Lisp
> unfortunatly nothing like the wished seems to be implemented, or
> (probably more likely) I did not have found it. Some months ago I
> wrote this:
> (defun* replace-in-files (from to &optional (in-files ".*\.html") 
[...]


> Well this obviously does not what was asked for, but it shows how to
> start the recursion. It's dept-first, but I assume that does not
> matter really. 
[...]

FWIW, I've been using a utility macro for operating on directory
trees:

(defun addinfo-subdirs (dir)
  "Return a list of direct child directories of directory DIR."
  (if (not (file-directory-p dir))
      (error "Not a directory: %s" dir)
    (cddr ; Get rid of "." and ".."
     (delq nil (mapcar (lambda (f)
                         (and (file-directory-p f)
                              f))
                       (directory-files dir t))))))

(defmacro do-addinfo-dir-tree (spec &rest body)
  "Run BODY once for each directory below DIRECTORY.
This traverses the directory tree below DIRECTORY (including)
depth first, binding VARIABLE to each directory name in turn.

\(fn (VARIABLE DIRECTORY) BODY)"
  (let ((var (car spec))
        (start-dir (cadr spec))
        (dir (make-symbol "--dir"))
        (subdirs (make-symbol "--subdirs"))
        (dir-queue (make-symbol "--dirqueue")))
    `(let ((,dir ,start-dir)
           ,subdirs ,dir-queue ,var)
       (unless (file-directory-p ,dir)
         (error "Not a directory: %s" ,dir))
       (while ,dir
         (setq ,subdirs (addinfo-subdirs ,dir)
               ,var ,dir)
         ,@body
         (cond (,subdirs
                (setq ,dir-queue (nconc (cdr ,subdirs)
                                        ,dir-queue)
                      ,dir (car ,subdirs)))
               (,dir-queue
                (setq ,dir (car ,dir-queue)
                      ,dir-queue (cdr ,dir-queue)))
               (t (setq ,dir nil)))))))


Example: to find all files with the extension ".info" below
$HOME, you could use it like this:

(let ((info-files nil))
  (do-addinfo-dir-tree (dir "~")
    (let ((files (delq nil
                       (mapcar (lambda (el)
                                 (and (not (file-directory-p el))
                                      el))
                               (directory-files dir)))))
      (dolist (f files)
        (when (string-match "\\.info\\'" f)
          (push f info-files)))))
  (nreverse info-files))

    Oliver
-- 
16 Brumaire an 213 de la Révolution
Liberté, Egalité, Fraternité!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]