From: Oliver Scholz <alkibiades@gmx.de>
Subject: Re: Where to start with recursive file operations
Date: Sat, 06 Nov 2004 07:47:43 +0100 [thread overview]
Message-ID: <87actvbhio.fsf@ID-87814.user.uni-berlin.de> (raw)
In-Reply-To: 87sm7s670u.fsf@fbigm.here
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é!
prev parent reply other threads:[~2004-11-06 6:47 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-31 0:20 Where to start with recursive file operations Andrei Stebkov
2004-10-31 2:55 ` Pascal Bourguignon
2004-11-01 17:22 ` Kevin Rodgers
2004-11-02 7:32 ` Friedrich Dominicus
2004-11-06 6:47 ` Oliver Scholz [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87actvbhio.fsf@ID-87814.user.uni-berlin.de \
--to=alkibiades@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.