find-files in the mzlib file.ss library is a very flexible way to do this. find-files takes 2 arguments, a one argument function that accepts a file name (full path) and returns #t if the file should be included in the result list, and the path to root the search at. If you omit the 2nd argument, or specify #f, find-files starts at the current directory. A few examples:
; list all files and directories under /tmp
(find-files (lambda (x) x) "/tmp")
; list all subdirectories of /tmp
(find-files (lambda (x) (directory-exists?x)) "/tmp")
; list all files with extension .scm under /tmp
(find-files
(lambda (x)
(and (equal? (filename-extensionx) "scm")
(not (directory-exists?x))))
"/tmp")
; list all files under /tmp that are > 1024 bytes
(find-files
(lambda (x) (and (not (directory-exists?x))
(> (file-sizex) 1024)))
"/tmp")
The examples given all just act on metadata of the file, there's no reason you can't inspect the contents of the file in the filter function, though that will make find-files much slower. Also, it can be a little tricky to get find-files to not recurse directory trees, at least on Windows. Generally, you include a directory-exists? test in your filter:
; list files only in the \windows directory, don't recurse directories.
(find-files
(lambda (x)
(and (not (directory-exists?x))
(equal? (path-onlyx) "\\windows\\")))
"\\windows")
fold-files, also in file.ss is a more powerful version of find-files.
-- GordonWeakliem - 29 Apr 2004