[REBOL] Re: Filtering files in directories.
From: tim:johnsons-web at: 2-Jan-2003 9:17
Hi Ed:
Code follows: Note that it uses a non-native 'rebol subroutine schema
but those dependencies are included as well.
I hope this is of some help.
; ---------------------------------------------------------------------------------------------------
; read-dir: Get file listing. "" or "*" returns all. Passing an extension returns
; a filtered list. /only strips extension from the list.
; code for 'sub follows
; ---------------------------------------------------------------------------------------------------
read-dir: sub[extension[string!] /only][
if extension/1 = #"*"[extension: next extension]
file-list: read %.
either empty? extension[file-list][
filtered: copy []
repeat file file-list[
if found: find/last to-string file extension[
if found = extension[ ; extension is last
either only[
append filtered copy/part file (index? found) - 1
][append filtered file]
]
] ] filtered ]
];end func
; ---------------------------------------------------------------------------------------------------
; below is the code for 'sub, which is 'aliased' from Andrew's 'Fun function
; If you chose not to use 'sub, then change read-dir to 'func and add the
; /local keyword to set your own local variables
; ---------------------------------------------------------------------------------------------------
Fun: function [
"Automatic local word generation for a function." [catch]
Spec [block!] {Optional help info followed by arg words (and optional type and string).}
Body [block!] "The body block of the function."
][ Locals LocalRefinement ][
throw-on-error [
;Locals: make block! 0
Locals: copy[]
if found? LocalRefinement: find Spec /local [
insert tail Locals next LocalRefinement
Spec: copy/part Spec LocalRefinement
]
foreach Value Body [
if all[set-word? :Value
not found? find Spec to refinement! :Value
][insert tail Locals to word! :Value ] ]
Locals: exclude Locals Spec
function Spec Locals Body
]
] sub: :Fun
* Ed Dana <[EDanaII--Cox--net]> [030102 08:59]:
> OK, time to tap the brains of the Gurus again. :)
>
> How do I get only specified files in a directory? IOW, how do I do the
> equivalent of pattern matching, if I want all JPG files, how do I return
> only *.jpg?
>
> So far, all the examples I have found involve loading all the files
> first and then extracting the ones you need or not from the series that
> is returned.
>
> Thanks, as always...
>
> --
> Sincerely, |
> Ed Dana | This is the worst kind of discrimination! The
> Software Developer | kind against me!
> 1Ghz Athlon Amiga | -- Bender, Futurama.
> =========== http://members.cox.net/edanaii/Home/Default.html >
>
> --
> To unsubscribe from this list, please send an email to
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com
http://www.johnsons-web.com