[plt-scheme] Fooling around w/files and directories

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Jun 22 23:09:40 EDT 2009

Sorry, dir.ss is for beginners. Here is small play-around example:

(define d:raw (directory-list)) ;; d:raw is just a name

(define d:txt (map path->string d:raw))

(define files-ending-in-tex (filter (lambda (f) (regexp-match ".tex"  
f)) d:txt))

(apply + (map file-size files-ending-in-tex))

And here is an excerpt from a script I wrote yesterday:

;; String -> [Listof String]
;; retrieve all numbered jpgs in a directory in the order of the  
numbers they contain
(define (get-jpgs dir)
   (parameterize ([current-directory dir])
     (define d (map path->string (directory-list)))
     (define e (filter (lambda (f) (regexp-match "\\.jpg$" f)) d))
     (define f (sort e (lambda (f g)
                         (define fn (string->number (second (regexp- 
match "([0-9]+)" f))))
                         (define gn (string->number (second (regexp- 
match "([0-9]+)" g))))
                         (< fn gn))))

;; I could have passed dir to directory-list in this case. Experiment!

-- Matthias

On Jun 22, 2009, at 10:07 PM, John Chandler wrote:

> There are occasionally little things I like to automate, even just
> once, because I need to perform the same tedious operation on 25
> separate files, or something.  And I thought that actually wanting to
> accomplish something might be a good motivation for learning at least
> certain aspects of Scheme.  The Teachpack, dir.ss seems to offer
> what's needed, but when I try to follow the doc for this, I hit an
> error right away.  It seems quite likely that I am *completely*
> missing a key concept here, because I was trying to follow slavishly
> hoping I'd catch on to the underlying principles eventually.  Blind
> pig, acorn, etc.
> I added the teachpack, dir.ss, clicked Run, and not a whole lot
> happened.  I looked at the next step in the doc, and it seemed to show
> a number of s-exps right after a ">".  So I typed in the first s-exp,
> and pressed Return.  I got this:
>     directory-list: could not open "/.Spotlight-V100/" (Permission
> denied; errno=13)
> The same thing happened when I pasted the whole set of s-exps into the
> upper window and clicked Run again.
> As I say, this failure is so complete that I think I must have missed
> something crucial early on, but I confess I don't have the patience to
> run through all the picture-drawing stuff right now, just in case the
> first lesson contains the answer.  I did skim that lesson and I didn't
> find the clue I needed.
> So, OK, I am clue-challenged, and I hope someone out there has one to
> toss me.
> The main thing is, I want to fool with files and directories, get info
> on files, e.g., size, remove and add file suffixes, maybe get
> checksums on files, etc.  I'd like to see how easy or horrible that is
> with Scheme.  I thought dir.ss might be the place to start, but at
> this point, everything I know could be wrong.
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.