Filtering files in directories.
[1/12] from: edanaii::cox::net at: 2-Jan-2003 10:26
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.
[2/12] 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
<<quoted lines omitted: 14>>
> [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
[3/12] from: greggirwin:mindspring at: 2-Jan-2003 11:48
Hi Ed,
ED> How do I get only specified files in a directory? IOW, how do I do the
ED> equivalent of pattern matching, if I want all JPG files, how do I return
ED> only *.jpg?
ED> So far, all the examples I have found involve loading all the files
ED> first and then extracting the ones you need or not from the series that
ED> is returned.
You can open a directory port directly if you want, though I don't
think it will gain you much in this case. Under Windows you can use
the FindFirstFile and FindNextFile APIs but that locks you in to the
OS.
Is performance an issue, or is it just curiosity?
-- Gregg
[4/12] from: robert:muench:robertmuench at: 2-Jan-2003 19:45
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
<<quoted lines omitted: 5>>
> I do the equivalent of pattern matching, if I want all JPG files,
> how do I return only *.jpg?
Hi, this isn't part of Rebol's base functions. There is a library named
something like directory-functions or file-functions written by (sorry
can't remember) that has a LS clone function in it. Very useful. Robert
[5/12] from: chris:ross-gill at: 2-Jan-2003 14:42
Hi Ed,
Ok, this is neither pattern matching (works by suffix) nor does it bypass
the need to load a full list of files (not sure there's any other way to do
it), but it can be hacked relatively easily for your own needs.
Usage:
>> read-filter %./ %.jpg
>> read-filter %./ [%.html %.r]
It also retains directories, but that could be dropped from the source.
- Chris
--
read-filter: func [
loc [file! url!] "Path to Read"
sfx [file! block!] "Suffix to Search For"
/local files
][
return either exists? loc: dirize loc [
files: read loc
remove-each file files [
not any [ ; Conditions to retain files
dir? join loc file
sfx = suffix? file
find sfx suffix? file
]
]
files
][
none
]
]
[6/12] from: vado:fabrice:chello at: 2-Jan-2003 20:18
Hi Ed,
Le Thursday, January 2, 2003, 6:26:01 PM, vous écriviez :
Ed> How do I get only specified files in a directory? IOW, how do I do
Ed> the equivalent of pattern matching, if I want all JPG files, how
Ed> do I return only *.jpg?
Just for fun (no, I'm REALLY not an expert ;) I have test that :
------------------------------
foreach file load %. [if find file ".jpg" [print file] ]
------------------------------
Just a start for your own function.
--
Fabrice
[7/12] from: al:bri:xtra at: 3-Jan-2003 10:12
Ed wrote:
> 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.
>> map read %. func [File [file!]] [if %.r = suffix? File [File]]
== [%docs.r %feedback.r %rebol.r %scripts.r %user.r]
Of course, you'll need my 'map and 'arguments functions.
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[8/12] from: edanaii:cox at: 3-Jan-2003 7:58
Gregg Irwin wrote:
>You can open a directory port directly if you want, though I don't
>think it will gain you much in this case. Under Windows you can use
>the FindFirstFile and FindNextFile APIs but that locks you in to the
>OS.
>
>Is performance an issue, or is it just curiosity?
>
Simple curiosity.
I kinda figured I would have to write something myself. I was just
hoping I was missing something.
Good responses from everyone. Thanks all.
--
Sincerely, |
Ed Dana | If Stupidity got us into this mess, then why can't
Software Developer | it get us out?
1Ghz Athlon Amiga | -- Will Rogers.
[9/12] from: vado:fabrice:chello at: 3-Jan-2003 16:21
Hi Andrew,
Andrew> Of course, you'll need my 'map and 'arguments functions.
Where can we get them ?!?
Here I don't find them http://valley.150m.com/
--
Fabrice
[10/12] from: al:bri:xtra at: 4-Jan-2003 8:38
> Andrew> Of course, you'll need my 'map and 'arguments functions.
>
> Where can we get them ?!?
You can get all of them from here:
http://valley.150m.com/Rebol/Values.r
I'll have to make it easier to find.
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[11/12] from: rebol-list2:seznam:cz at: 3-Jan-2003 12:16
Hello Gregg,
Thursday, January 2, 2003, 7:48:18 PM, you wrote:
GI> Hi Ed,
ED>> How do I get only specified files in a directory? IOW, how do I do the
ED>> equivalent of pattern matching, if I want all JPG files, how do I return
ED>> only *.jpg?
ED>> So far, all the examples I have found involve loading all the files
ED>> first and then extracting the ones you need or not from the series that
ED>> is returned.
GI> You can open a directory port directly if you want, though I don't
GI> think it will gain you much in this case. Under Windows you can use
GI> the FindFirstFile and FindNextFile APIs but that locks you in to the
GI> OS.
GI> Is performance an issue, or is it just curiosity?
GI> -- Gregg
I've tried to open very large directory (more then 2700 files) directly using:
files: open/direct %very/large/dir/
and it looks that the output is buffered as well (at least when I
use "? dir" I can see all the dir file names in the dir/state/inBuffer
so you can use:
forall files [probe files/1] ;to list all file names
close files
but it looks it's same as simple:
files: read %very/large/dir/
the open/skip on directory is not working as well
=( Oliva David )=======================( [oliva--david--seznam--cz] )==
=( Earth/Europe/Czech_Republic/Brno )=============================
=( coords: [lat: 49.22 long: 16.67] )=============================
[12/12] from: greggirwin:mindspring at: 4-Jan-2003 11:36
Thansk Oldes,
I haven't done any tests. On a dir with ~2,000 files here the response
is very fast just using READ. The place it would show a difference is
if you have a utility that searches all files on a drive by date,
size, etc. For that you'd be reading the dir, then reading the info
for each file. If you use the Win API (only for Windows of course) you
get the info as you find the file which saves lots of reads. I'd do it
the easy wasy first though, and only if it wasn't fast enough would I
consider going the other way.
-- Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted