guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

here is one way to implement file globbing


From: tantalum
Subject: here is one way to implement file globbing
Date: Mon, 15 Oct 2018 23:12:43 +0000
User-agent: Posteo Webmail

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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]