s c h e m a t i c s : c o o k b o o k

/ Cookbook.FileDirectorySize

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Computing the Size of a Directory

Problem

You want to compute the total size of a directory tree (i.e. the sum of the file sizes in a directory tree).

Solution

(require (lib "file.ss"))
(fold-files (lambda (f type a) 
              (if (eq? type 'file) 
                  (+ a (file-size f)) 
                  a))
            0 
            path)

Discussion

fold-files in PLT's file.ss pretty much has this built in. All you need to add is a procedure to accumulate the size of the files.

For a little twist, here's a version that collects the total file size in the car of a pair and the number of files in the cdr:

(fold-files (lambda (f type a) 
              (let ((size (car a))
                    (cnt (cdr a)))
                (if (eq? type 'file)
                    (begin
                      (set-car! a (+ size (file-size f)))
                      (set-cdr! a (+ cnt 1))
                      a)
                    a)))
            (cons 0 0)
            path)


Comments about this recipe

Contributors

-- GordonWeakliem - 13 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest