Discussion:
here is one way to implement file globbing
tantalum
2018-10-15 23:12:43 UTC
Permalink
im sure it is not the optimal way to do it, and currently it fails on
file access errors and there might be bugs, but it has cool features and
i just wanted to share.
filesystem globbing is the matching of paths that can include wildcard
characters like asterisks.

the code implements the following:

filesystem-glob :: string -> (string ...)

find files matching a file system path with optional wildcard
characters.
* matches zero or more of any character in a file name.
? matches one of any character in a file name.
** skips any sub directories to match the rest of the path. at the end
of a path it is the same as **/.* including .*
**n where n is an integer. like ** but skips directories at most n sub
directories deep.
example patterns
a/b/*
*.txt
a/**/c/*.txt
a/**
**/*.txt
a/**2/*

to list all files in a directory recursively: dir/**
and to list all pdf files in a directory recursively: dir/**/*.pdf

here is the code with reduced dependencies so that anybody with only
guile should be able to run it:
http://files.sph.mn/s/computer/guile-fsg.scm

it is now part of sph-lib:
https://github.com/sph-mn/sph-lib/blob/master/modules/sph/filesystem.scm

you can play around with it on the command-line by creating an
executable file with content similar to this:

#!/usr/bin/guile
!#

(include "guile-fsg.scm")

(for-each (lambda (a) (display a) (newline))
(filesystem-glob (car (cdr (program-arguments)))))


if this is in a file "mygl" then you can call it like "./mygl '/tmp/*'".
paths in single quotes, otherwise the shell evaluates the wildcards.
Amirouche Boubekki
2018-10-16 15:12:25 UTC
Permalink
Post by tantalum
im sure it is not the optimal way to do it, and currently it fails on
file access errors and there might be bugs, but it has cool features
and i just wanted to share.
[...]
Post by tantalum
here is the code with reduced dependencies so that anybody with only
http://files.sph.mn/s/computer/guile-fsg.scm
https://github.com/sph-mn/sph-lib/blob/master/modules/sph/filesystem.scm
Thanks for sharing.

Loading...