all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* traverse a directory
@ 2008-01-01 16:51 Xah Lee
  2008-01-01 20:22 ` Eli Zaretskii
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Xah Lee @ 2008-01-01 16:51 UTC (permalink / raw)
  To: help-gnu-emacs

if i want to recurse into a directory, do i have to write my own?

e.g. i want to apply a file processing function to all files in a dir
and subdirs. Somethnig like

(apply 'process-my-file "/Users/xah/emacs/" "\.html$")

I noticed that directory-files only list the dir file. Someone told me
there's a directory-files-recur but that doesn't seems to be in emacs.

thx in advance.

  Xah
  xah@xahlee.org
\xAD\xF4 http://xahlee.org/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-01 16:51 traverse a directory Xah Lee
@ 2008-01-01 20:22 ` Eli Zaretskii
  2008-01-02  5:43 ` Mike Mattie
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Eli Zaretskii @ 2008-01-01 20:22 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Xah Lee <xah@xahlee.org>
> Date: Tue, 1 Jan 2008 08:51:33 -0800 (PST)
> 
> if i want to recurse into a directory, do i have to write my own?

Take a look at the package find-lisp.el, which is bundled with Emacs
22.  It has infrastructure for what you want.  In particular, there's
the `find-lisp-find-files-internal' function that recursively
traverses a directory.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-01 16:51 traverse a directory Xah Lee
  2008-01-01 20:22 ` Eli Zaretskii
@ 2008-01-02  5:43 ` Mike Mattie
       [not found] ` <mailman.5612.1199252679.18990.help-gnu-emacs@gnu.org>
       [not found] ` <mailman.5595.1199218943.18990.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 12+ messages in thread
From: Mike Mattie @ 2008-01-02  5:43 UTC (permalink / raw)
  To: help-gnu-emacs


[-- Attachment #1.1: Type: text/plain, Size: 3141 bytes --]

On Tue, 1 Jan 2008 08:51:33 -0800 (PST)
Xah Lee <xah@xahlee.org> wrote:

> if i want to recurse into a directory, do i have to write my own?
> 
> e.g. i want to apply a file processing function to all files in a dir
> and subdirs. Somethnig like
> 
> (apply 'process-my-file "/Users/xah/emacs/" "\.html$")
> 
> I noticed that directory-files only list the dir file. Someone told me
> there's a directory-files-recur but that doesn't seems to be in emacs.
> 
> thx in advance.

here is something kinda related that I recently hacked up. I needed to be able to
filter the results of a directory list. The first hack started off as a plain
defun but I encountered variations so I made a macro. it may duplicate existing
emacs code.

the elisp-in-path defun shows how the form is used. It's uncommented, and
relies upon map-filter-nil which needs to be non-recursive for general use.

It is pretty easy to extend for filtering on either the path or the mode string.
Writing a general tree recursion is trivial, it shouldn't even be file specific.
this defun may help tailor such a recursion towards a fs tree.

(defun filter-ls-attributes ( filter-form )
  "implement the various attribute filters for the filter-ls form"
  (lexical-let
    ((attr-name (symbol-name (car filter-form)))
      (attr-match (cadr filter-form)))

    (cond
      ((string-equal "type" attr-name) (list 'char-equal attr-match  '(aref (cdr path-pair) 0)))
      ((string-equal "path" attr-name) (list 'string-match-p attr-match '(car path-pair)))
    ;; error path
    )))

(defmacro filter-ls (path path-type &rest filters)
  "a form for flexibly filtering the result of listing a directory with attributes"
  `(apply 'map-filter-nil
     (lambda ( path-pair )
       (if ,(cons 'and (mapcar 'filter-ls-attributes filters))
         (car path-pair)))

     ;; reduce the attributes to a pair of the path, and the mode string
     (mapcar (lambda ( attr-list )
               (cons (car attr-list) (nth 9 attr-list)))
       ;; get the list of files.
       (directory-files-and-attributes ,path ,path-type))
     ))

;; path-type is t absolute, nil relative.

(defun elisp-in-path ( path path-type )
  "return a list of elisp files in the path"

  (filter-ls path path-type
    (type ?-)
    (path "\\.el$")))

P.S

here is map-filter-nil. use at your own risk.

(defun map-filter-nil ( func &rest seq )
  "map-filter-nil. apply the function to the arguements ala mapcar.
   Filter any nil elements of the sequence before the function is
   applied, and after the function is applied."

  (if (car seq)
    (let
      ((result (funcall func (car seq))))
      (if result
        (cons result (apply 'map-filter-nil func (cdr seq)))
        (apply 'map-filter-nil func (cdr seq))
        ))
    (if (cdr seq)
      (apply 'map-filter-nil func (cdr seq)))
    ))

>   Xah
>   xah@xahlee.org
> ∑ http://xahlee.org/
> 
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 152 bytes --]

_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
       [not found] ` <mailman.5612.1199252679.18990.help-gnu-emacs@gnu.org>
@ 2008-01-02 18:03   ` Joel J. Adamson
  2008-01-02 19:04     ` Mike Mattie
       [not found]     ` <mailman.5637.1199300789.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 12+ messages in thread
From: Joel J. Adamson @ 2008-01-02 18:03 UTC (permalink / raw)
  To: help-gnu-emacs

Mike Mattie <codermattie@gmail.com> writes:

> On Tue, 1 Jan 2008 08:51:33 -0800 (PST)
> Xah Lee <xah@xahlee.org> wrote:
>
>> if i want to recurse into a directory, do i have to write my own?

I know that doing this in Emacs is especially cool, but "find ... exec {}\;"
should do the trick.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-02 18:03   ` Joel J. Adamson
@ 2008-01-02 19:04     ` Mike Mattie
       [not found]     ` <mailman.5637.1199300789.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 12+ messages in thread
From: Mike Mattie @ 2008-01-02 19:04 UTC (permalink / raw)
  To: help-gnu-emacs


[-- Attachment #1.1: Type: text/plain, Size: 600 bytes --]

On Wed, 02 Jan 2008 13:03:39 -0500
jadamson@partners.org (Joel J. Adamson) wrote:

> Mike Mattie <codermattie@gmail.com> writes:
> 
> > On Tue, 1 Jan 2008 08:51:33 -0800 (PST)
> > Xah Lee <xah@xahlee.org> wrote:
> >
> >> if i want to recurse into a directory, do i have to write my own?
> 
> I know that doing this in Emacs is especially cool, but "find ...
> exec {}\;" should do the trick.
> 
> Joel
> 

cool maybe, but portable was my reason for doing it this way. BSD find is very different from gnu find. 
some systems don't even have find. exec is a jungle, posix a mirage :)

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 152 bytes --]

_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
       [not found]     ` <mailman.5637.1199300789.18990.help-gnu-emacs@gnu.org>
@ 2008-01-02 19:25       ` Joel J. Adamson
  2008-01-03  5:51         ` Xah Lee
  2008-01-05  0:46         ` Mike Mattie
  0 siblings, 2 replies; 12+ messages in thread
From: Joel J. Adamson @ 2008-01-02 19:25 UTC (permalink / raw)
  To: help-gnu-emacs

Mike Mattie <codermattie@gmail.com> writes:

> On Wed, 02 Jan 2008 13:03:39 -0500
> jadamson@partners.org (Joel J. Adamson) wrote:
>
>> Mike Mattie <codermattie@gmail.com> writes:
>> 
>> > On Tue, 1 Jan 2008 08:51:33 -0800 (PST)
>> > Xah Lee <xah@xahlee.org> wrote:
>> >
>> >> if i want to recurse into a directory, do i have to write my own?
>> 
>> I know that doing this in Emacs is especially cool, but "find ...
>> exec {}\;" should do the trick.
>> 
>> Joel
>> 
>
> cool maybe, but portable was my reason for doing it this way. BSD find
> is very different from gnu find.

So I've heard: should I go ahead with installing FreeBSD?

> some systems don't even have find. exec is a jungle, posix a mirage :)

Some systems I've found aren't systematic.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
       [not found] ` <mailman.5595.1199218943.18990.help-gnu-emacs@gnu.org>
@ 2008-01-03  4:30   ` Xah Lee
  0 siblings, 0 replies; 12+ messages in thread
From: Xah Lee @ 2008-01-03  4:30 UTC (permalink / raw)
  To: help-gnu-emacs

Xah Lee wrote:
「if i want to recurse into a directory, do i have to write my own?」

Eli Zaretskii <e...@gnu.org> wrote:
「Take a look at the package find-lisp.el, which is bundled with Emacs
22.  It has infrastructure for what you want.  In particular, there's
the `find-lisp-find-files-internal' function that recursively
traverses a directory.」

Thanks. That does it.

(require 'find-lisp)
(find-lisp-find-files "~/web/" "\\.html$")

  Xah
  xah@xahlee.org
\xAD\xF4 http://xahlee.org/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-02 19:25       ` Joel J. Adamson
@ 2008-01-03  5:51         ` Xah Lee
  2008-01-03 11:16           ` Alexey Pustyntsev
                             ` (2 more replies)
  2008-01-05  0:46         ` Mike Mattie
  1 sibling, 3 replies; 12+ messages in thread
From: Xah Lee @ 2008-01-03  5:51 UTC (permalink / raw)
  To: help-gnu-emacs

about traversing a dir tree...

In Python, it's done with “walk”, like this:
os.path.walk(mydir, myfun, 'somenull')

In Perl, it's done with File::Find qw(find), like this:
find(\&myfun, $mydir);

which is quite fucked up.

For details on Python and Perl's dir traverse, see

★ Perl-Python Tutorial: Traverse A Directory
http://xahlee.org/perl-python/traverse_dir.html

For unix, as you said, is “find” (with exec or xargs), which is the
most fucking fuck demented twisted crippled shit in the entire
universe. (This point is particularly interesting because most senior
unix sys admins and don't realize it but actually thought its godsend)
For more, see:

★ Unix Command Line Tools Tips
http://xahlee.org/UnixResource_dir/unix_tips.html

Emacs lisp's
(require 'find-lisp)
(mapcar 'myfun (find-lisp-find-files mydir))
Is the most beautiful among these.

For a general treatise on sorting tree nodes (i.e. the order of
visiting), see:

★ Sorting Tree Indexes
http://xahlee.org/tree/a-IndexSetSort.html

  Xah
  xah@xahlee.org
\xAD\xF4 http://xahlee.org/


On Jan 2, 11:25 am, jadam...@partners.org (Joel J. Adamson) wrote:
>...
> > cool maybe, but portable was my reason for doing it this way. BSD find
> > is very different from gnu find.
>
> So I've heard: should I go ahead with installing FreeBSD?
>
> > some systems don't even have find. exec is a jungle, posix a mirage :)
>
> Some systems I've found aren't systematic.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-03  5:51         ` Xah Lee
@ 2008-01-03 11:16           ` Alexey Pustyntsev
  2008-01-03 16:25           ` Joel J. Adamson
       [not found]           ` <bf802645-6c07-46d6-b968-1067c1ff4c7f@d21g2000prf.googlegro ups.com>
  2 siblings, 0 replies; 12+ messages in thread
From: Alexey Pustyntsev @ 2008-01-03 11:16 UTC (permalink / raw)
  To: help-gnu-emacs

Xah Lee <xah@xahlee.org> writes:

> For unix, as you said, is “find” (with exec or xargs), which is the
> most fucking fuck demented twisted crippled shit in the entire
> universe. (This point is particularly interesting because most senior
> unix sys admins and don't realize it but actually thought its godsend)
> For more, see:
>
> ★ Unix Command Line Tools Tips
> http://xahlee.org/UnixResource_dir/unix_tips.html
>
> Emacs lisp's
> (require 'find-lisp)
> (mapcar 'myfun (find-lisp-find-files mydir))
> Is the most beautiful among these.
>
> For a general treatise on sorting tree nodes (i.e. the order of
> visiting), see:

Dude, please, calm down. Relax. 

-- 
Rgds
Alexey

Happy New Year!

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-03  5:51         ` Xah Lee
  2008-01-03 11:16           ` Alexey Pustyntsev
@ 2008-01-03 16:25           ` Joel J. Adamson
       [not found]           ` <bf802645-6c07-46d6-b968-1067c1ff4c7f@d21g2000prf.googlegro ups.com>
  2 siblings, 0 replies; 12+ messages in thread
From: Joel J. Adamson @ 2008-01-03 16:25 UTC (permalink / raw)
  To: help-gnu-emacs

Xah Lee <xah@xahlee.org> writes:

> For unix, as you said, is “find” (with exec or xargs), which is the
> most fucking fuck demented twisted crippled shit in the entire
> universe.

Really?  I was unaware of that fact.  Thanks for letting me know.  I'll
keep it in mind.

Any other fucking fuck demented twisted crippled shit in the entire
universe you'd like to let me know about?

Thanks,
Joel
-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: traverse a directory
  2008-01-02 19:25       ` Joel J. Adamson
  2008-01-03  5:51         ` Xah Lee
@ 2008-01-05  0:46         ` Mike Mattie
  1 sibling, 0 replies; 12+ messages in thread
From: Mike Mattie @ 2008-01-05  0:46 UTC (permalink / raw)
  To: help-gnu-emacs


[-- Attachment #1.1: Type: text/plain, Size: 1179 bytes --]

On Wed, 02 Jan 2008 14:25:04 -0500
jadamson@partners.org (Joel J. Adamson) wrote:

> Mike Mattie <codermattie@gmail.com> writes:
> 
> > On Wed, 02 Jan 2008 13:03:39 -0500
> > jadamson@partners.org (Joel J. Adamson) wrote:
> >
> >> Mike Mattie <codermattie@gmail.com> writes:
> >> 
> >> > On Tue, 1 Jan 2008 08:51:33 -0800 (PST)
> >> > Xah Lee <xah@xahlee.org> wrote:
> >> >
> >> >> if i want to recurse into a directory, do i have to write my
> >> >> own?
> >> 
> >> I know that doing this in Emacs is especially cool, but "find ...
> >> exec {}\;" should do the trick.
> >> 
> >> Joel
> >> 
> >
> > cool maybe, but portable was my reason for doing it this way. BSD
> > find is very different from gnu find.
> 
> So I've heard: should I go ahead with installing FreeBSD?

I sure don't know. Whatever you like the best. I just post
code if I think it's apropos. Sometimes it is, and when
it isn't someone usually mentions where the canonical
implementation is located within the Emacs distribution.
 
> > some systems don't even have find. exec is a jungle, posix a
> > mirage :)
> 
> Some systems I've found aren't systematic.
> 
> Joel
> 

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 152 bytes --]

_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* OT: Re: traverse a directory
       [not found]           ` <bf802645-6c07-46d6-b968-1067c1ff4c7f@d21g2000prf.googlegro ups.com>
@ 2008-01-05 17:47             ` Johan Lindström
  0 siblings, 0 replies; 12+ messages in thread
From: Johan Lindström @ 2008-01-05 17:47 UTC (permalink / raw)
  To: help-gnu-emacs

At 04:51 2008-01-03, Xah Lee wrote:
>In Perl, it's done with File::Find qw(find), like this:
>find(\&myfun, $mydir);
>
>which is quite fucked up.

Yes, that interface surely shows its age. For a nicer experience, try:
http://search.cpan.org/perldoc?File::Find::Rule
or possibly
http://search.cpan.org/perldoc?File::Finder


/J

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2008-01-05 17:47 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-01 16:51 traverse a directory Xah Lee
2008-01-01 20:22 ` Eli Zaretskii
2008-01-02  5:43 ` Mike Mattie
     [not found] ` <mailman.5612.1199252679.18990.help-gnu-emacs@gnu.org>
2008-01-02 18:03   ` Joel J. Adamson
2008-01-02 19:04     ` Mike Mattie
     [not found]     ` <mailman.5637.1199300789.18990.help-gnu-emacs@gnu.org>
2008-01-02 19:25       ` Joel J. Adamson
2008-01-03  5:51         ` Xah Lee
2008-01-03 11:16           ` Alexey Pustyntsev
2008-01-03 16:25           ` Joel J. Adamson
     [not found]           ` <bf802645-6c07-46d6-b968-1067c1ff4c7f@d21g2000prf.googlegro ups.com>
2008-01-05 17:47             ` OT: " Johan Lindström
2008-01-05  0:46         ` Mike Mattie
     [not found] ` <mailman.5595.1199218943.18990.help-gnu-emacs@gnu.org>
2008-01-03  4:30   ` Xah Lee

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.