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: Friedrich Dominicus
Subject: Re: Where to start with recursive file operations
Date: Tue, 02 Nov 2004 08:32:01 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Security Through Obscurity, linux)

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. For instance, checks out the file (from CVS or
>  > Perforce), selects the whole buffer, indents the contents and save it.
>  > Also I'd like to have same kind of function that would search and
> replace in
>  > multiple files.
>  > As all the problems that I listed here stem from the same root (recursive
>  > search) I need a few pointers about the basic built-in emacs functions of
>  > this domain. The function of this nature must be already written, I just
>  > couldn't find 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:
>
> #!/bin/sh
>
> find . -type f -name "*.ext" -print |
> while IFS= read -r file; do
>    emacs --batch "$file" \
>       --eval "(vc-checkout buffer-file-name)" \
>       --eval "(indent-region (point-min) (point-max) nil)" \
>       --funcall save-buffer
> done
>
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") 
                               (directory ".") 
                               (recurse-p t))
  (let ((files (nnheader-directory-files directory)))
    (dolist (act-file files)
      (cond
       ((and recurse-p (file-directory-p act-file))
        (replace-in-files from to in-files act-file recurse-p))
       ((string-match in-files act-file)
        (find-file act-file)
        (replace-string from to)
        (save-buffer)
        (kill-buffer nil))
       (t ))))) ; fall through

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. 

The changes are not really big, drop the from to, add a function to do
the job and replace the stuff below (string-match with loading the
file and running the wished function on it...

Regards
Friedrich



-- 
Please remove just-for-news- to reply via e-mail.


reply via email to

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