2.1 (aux filesystem)

This module provides procedures to operate on URIs, files and directories.

2.1.1 Pseudo-data Types

List: L-Path

An L-Path is a list-based representation of a path to a directory or file in the file system. It can be one of:

  • (list String)
  • (cons String (list String ...))

For example:

 "intro.webm"
(list "intro.webm")

 "videos/intro.webm"
(list "videos" "intro.webm")

 "/home/ana/videos/intro.webm"
(list "" "home" "ana" "videos" "intro.webm")

 "/home/ana/videos/"
(list "" "home" "ana" "videos" "")

2.1.2 Procedures

Procedure: abbreviate-path path

Replace the user home directory in the given path with a tilde (~).

Examples:

> (abbreviate-path "/home/bob/Documents/essays/robots.org")
"~/Documents/essays/robots.org"

Procedure specification:

path (string): Absolute path to anything in the file system.

Return value (string): A new path where ~ replaces the user home directory if the user home directory in path matches the value of the HOME environment variable.

Procedure: directories directory

Return a list of paths to all the directories in the given directory.

Examples:

> (directories "/home/user")
(list "/home/user/images/nature"
      "/home/user/images/vehicles"
      "/home/user/images/drawings")

Procedure specification:

directory (string): Absolute or relative path to a directory.

Return value (list of strings): A list of paths to the directories in directory.

Procedure: directory? path

Tell whether the given path leads to a directory.

Examples:

> (directory? "/home/bob/videos")
#t
> (directory? "images/sky/sunset.png")
#f

Procedure specification:

path (string): An absolute or relative path to something in the file system.

Return value (boolean): True if the path leads to a directory; false otherwise.

Procedure: extension? path extension

Return true if the file in path has the given extension.

Examples:

> (define path "/home/joe/intro.webm")
> (extension? path "webm")
#t

Procedure specification:

path (string): An absolute or relative path to a file.

extension (string): A file extension (without the dot). For example: "webm", "xml".

Return value (boolean): True if the file in path has the given extension. Otherwise, false.

Procedure: find-files path extension OPT: skip-dirs

Return a list of paths to all the files in the directory indicated by path that have the given extension. The files are looked up recursively.

Examples:

> (find-files "/home/user/docs" "pdf")

Procedure specification:

path (string): An absolute or relative path to a directory.

extension (string): A file extension (without the dot). For example: "pdf", "png".

skip-dirs (list of string): An optional list of directory names to skip. For example:

  (list ".git" ".hg" ".svn" "drafts")
  

Return value (list of string): A list of strings representing absolute paths to all the files matching the extension. If no files are found, the list is empty.

Procedure: join-path . args

Return a file path string constructed from the args.

Usage examples:

> (join-path "jane" "Documents" "report.odt")
"jane/Documents/report.odt"
> (join-path "" "home" "jane" "Documents")
"/home/jane/Documents"
> (join-path "" "home" "jane" "")
"/home/jane/"

Procedure specification:

args (strings): A number of strings that represent parts of a path to a directory or file. Like the elements of an L-Path.

Return value (string): Path to a file or directory.

Procedure: list->path nodes

Return a file path string constructed from the list of nodes.

Usage examples:

> (list->path (list "jane" "Documents" "report.odt"))
"jane/Documents/report.odt"
> (list->path (list "" "home" "jane" "Documents"))
"/home/jane/Documents"
> (list->path (list "" "home" "jane" ""))
"/home/jane/"

Procedure specification:

nodes (list of string): Every string represents a part of a path to a file or directory. To indicate an absolute path, use an empty string as the first element of the list. To end the path with a file system separator, use an empty string as the last element of the list.

Return value (string): Path to a file or directory.

Procedure: makedir directory

Create directory and its parents if they don’t exist.

directory (string): Absolute or relative path to the desired directory. For example:

  (makedir "Projects/games/zeven")
  

Result Create the directory and its parent directories if they don’t exist. For example, given the directory of the previous example, create

  • Projects
  • Projects/games
  • Projects/games/zeven
Procedure: parent-dirs path

Return the list of parent directories of the file or directory indicated by the given path.

Examples:

> (parent-dirs "/home/user/videos/rain.webm")
(list "/"
      "/home"
      "/home/user"
      "/home/user/videos")

Procedure specification:

path (string): Absolute or relative path to a file or directory.

Return value (list of strings): List of paths to the parent directories of the given path.

Procedure: parent-dirs* path

Return the list of parent directories of the file or directory indicated by the given path.

Examples:

> (parent-dirs* (list "" "home" "user" "videos" "rain.webm"))
(list '("")
      '("" "home")
      '("" "home" "user")
      '("" "home" "user" "videos"))

path (L-Path): Path to a file or directory.

Return value (list of L-Paths): List of paths to the parent directories of the given path.

Procedure: path->list path

Return a list of nodes from path.

Usage examples:

> (path->list "jane/Documents/report.odt")
(list "jane" "Documents" "report.odt")
> (path->list "/home/jane/Documents")
(list "" "home" "jane" "Documents")
> (path->list "/home/jane/")
(list "" "home" "jane" "")

Procedure specification:

path (string): Path to a file or directory.

Return value (list of string) List whose elements represent the nodes of the path.