List of files matching a pattern

Q&A's, tips, howto's
Locked
HJH
Posts: 21
Joined: Tue May 31, 2005 2:21 pm
Location: Europe

List of files matching a pattern

Post by HJH »

Hi

I found a function which shows the current working directory on
http://www.newlisp.org/index.cgi?page=Code_Snippets
which is very handy

Is there a function which is similar to the Tcl glob function?
http://tmml.sourceforge.net/doc/tcl/glob.html

I would like to say

Code: Select all

(glob "*.txt")
and get back a list of all file names in the current working directory matching the pattern.
Perhaps somebody already has implemented something like this?

Thanks for the answer in advance.
HJH

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Use 'filter' to filter out the files you want

Code: Select all

> (filter (fn (f) (ends-with f ".c")) (directory))
("newlisp.c" "nl-count.c" "nl-debug.c" "nl-filesys.c" "nl-import.c" 
 "nl-list.c" "nl-liststr.c" "nl-math.c" "nl-matrix.c" "nl-sock.c" 
 "nl-string.c" "nl-symbol.c" "nl-utf8.c" "nl-web.c" "nl-xml.c" "osx-dlfcn.c" 
 "pcre-chartables.c" "pcre.c" "sql.c" "tru64.c" "types.c" "unix-lib.c" 
 "win32-util.c" "win32dll.c")
or on UNIX, this gives you the same:

Code: Select all

(exec "ls *.c")
I don't have a Windows machine close, but I think:

Code: Select all

(exec "dir /b")
would work, or may be you would have to call the shell:

Code: Select all

(exec "cmd /c dir /b")


Lutz

Locked