List of files matching a pattern

Started by HJH, May 31, 2005, 07:35:11 AM

Previous topic - Next topic

HJH

Hi



I found a function which shows the current working directory on

http://www.newlisp.org/index.cgi?page=Code_Snippets">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">http://tmml.sourceforge.net/doc/tcl/glob.html



I would like to say
(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

#1
Use 'filter' to filter out the files you want



> (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:

(exec "ls *.c")

I don't have a Windows machine close, but I think:

(exec "dir /b")

would work, or may be you would have to call the shell:

(exec "cmd /c dir /b")

 



Lutz