[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

11.6.1 Directory utilities

Function: current-directory &optional new-directory
When called with no argument, this returns the pathname of the current working directory. When called with a string argument new-directory, this sets the current working directory of the process to it. If the process can't change directory to new-directory, an error is signalled.

This function is in ChezScheme, MzScheme and some other Scheme implementations.

Function: home-directory &optional user
Returns the home directory of the given user, which may be a string user name or an integer user id. If user is omitted, the current user is assumed. If the given user cannot be found, or the home directory of the user cannot be determined, #f is returned.

Function: directory-list path &keyword children? add-path? filter
Returns a list of entries in the directory path. The result is sorted by dictionary order.

By default, only the basename (the last component) of the entries returned. If add-path? is given and true, path is appended to each entry. If children? is given and true, "." and ".." are excluded from the result. If filter is given, it must be a predicate that takes one argument. It is called on every element of the entry basename, and only the entries on which filter returns true are included in the result.

If path is not a directory, an error is signalled.

 
(directory-list "test")
 => ("." ".." "test.scm" "test.scm~")

(directory-list "test" :add-path? #t)
 => ("test/." "test/.." "test/test.scm" "test/test.scm~")

(directory-list "test" :children? #t)
 => ("test.scm" "test.scm~")

(directory-list "test" :children? #t :add-path? #t
   :filter (lambda (e) (not (string-suffix? "~" e))))
 => ("test/test.scm")

Function: directory-list2 path &keyword children? add-path? filter follow-link?
Like directory-list, but returns two values; the first one is a list of subdirectories, and the second one is a list of the rest. The keyword arguments children?, add-path? and filter are the same as directory-list.

Giving false value to follow-link? makes directory-list2 not follow the symbolic links; if the path contains a symlink to a directory, it will be included in the first list if follow-link? is omitted or true, while it will be in the second list if follow-link? is false.

Function: directory-fold path proc knil &keyword lister follow-link?
A fundamental directory traverser. Conceptually it works as follows, in recursive way.

The default procedure of lister is just a call to directory-list, as follows.
 
(lambda (path knil)
  (directory-list path :add-path? #t :children? #t)))))

Note that lister shouldn't return the given path itself (".") nor the parent directory (".."), or the recursion wouldn't terminate. Also note lister is expected to return a path accesible from the current directory, i.e. if path is "/usr/lib/foo" and it contains "libfoo.a" and "libfoo.so", lister should return '("/usr/lib/foo/libfoo.a" "/usr/lib/foo/libfoo.so").

The keyword argument follow-link? is used to determine whether lister should be called on a symbolic link pointing to a directory. When follow-link? is true (default), lister is called with the symbolic link if it points to a directory. When follow-link? is false, proc is not called.

The following examble returns a list of pathnames of the emacs backup files (whose name ends with "~") under the given path.
 
(use srfi-13) ;; for string-suffix?
(directory-fold path
                (lambda (entry result) 
                  (if (string-suffix? "~" entry)
                      (cons entry result)
                      result))
                '())

Function: make-directory* name &optional perm
Function: create-directory* name &optional perm
Creates a directory name. If the intermediate path to the directory doesn't exist, they are also created (like mkdir -p command on Unix). If the directory name already exist, these procedure does nothing. Perm specifies the integer flag for permission bits of the directory.

Function: remove-directory* name
Function: delete-directory* name
Deletes directory name and its content recursively (like rm -r command on Unix). Symbolic links are not followed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html