On this page:
path-contains?
make-directory-tree
make-non-conflicting-filename
make-non-conflicting-path
read-file->string
concatenate-files
directory-tree
in-directory
file-pretty-size
prettify-file-size

10 File and path utilities

 (require (planet untyped/unlib/file))
Utilities for manipulating files and paths.

(path-contains? path1 path2)  boolean?
  path1 : path?
  path2 : path?
Determines whether path2 is a subpath of path1.

Examples:

  > (path-contains? (build-path "/a/b/c")
                    (build-path "/a/b/c/d"))

  #t

  > (path-contains? (build-path "/a/b/c/d")
                    (build-path "/a/b/c"))

  #f

  > (path-contains? (build-path "/a/b/c")
                    (build-path "/a/b/c/d/../../d"))

  #f

  > (path-contains? (build-path "/a/b/c")
                    (build-path "/a/b/c/d/../../c/d"))

  #t

(make-directory-tree tree)  void?
  tree : folders-spec
Creates a directory tree in the current directory that matches tree, which is a tree of strings of type folders-spec:

folders-spec ::= (listof folder-spec)

folder-spec  ::= string folders-spec

tree-spec For example, the code:
  (make-directory-tree '("a" ("b" "c" ("d"))))
would create the directory tree:

/a

/a/b

/a/c

/a/c/d

Any existing directories in the tree are left intact.

(make-non-conflicting-filename path    
  filename)  string?
  path : (U path? string?)
  filename : string?
Returns a filename that is guaranteed to not conflict with the names of any files in path. For example:

  (make-non-conflicting-filename (string->path "mydir") "myfile.txt")
would return:
  • "myfile.txt" if "myfile.txt" doesn’t exist in "mydir";

  • "myfile1.txt" if "myfile.txt" does exist in "mydir";

  • "myfile2.txt" if "myfile.txt" and "myfile1.txt" both exist in "mydir";

  • and so on...

(make-non-conflicting-path path filename)  path?
  path : (U path? string?)
  filename : string?
Like make-non-conflicting-filename but returns:

  (build-path path (make-non-conflicting-filename path filename))

(read-file->string file)  string?
  file : (U path? string?)
Reads the contents of file into a string. See the port.plt collection on PLaneT for more advanced functions along these lines.

(concatenate-files dest sources)  void?
  dest : (U path? string?)
  sources : (listof (U path? string?))
Concatenates (appends) the contents of sources and writes the result to dest.

(directory-tree path    
  [#:order order    
  #:filter filter    
  #:follow-links? follow-links?])  (listof path?)
  path : (U path? string?)
  order : (U 'pre 'post) = 'pre
  filter : (path? -> boolean?) = (lambda (path) #t)
  follow-links? : boolean? = #t
Returns a list of absolute paths of all matching files/directories/links in path or subdirectories thereof.

The order argument specifies whether the directory tree should be traversed in pre- or post-order; the filter argument specifies a predicate which returned results much match; the follow-links? argument specifies whether links to directories should be treated as directories or atomic files.

(in-directory path    
  [#:order order    
  #:filter filter    
  #:follow-links? follow-links?])  sequence?
  path : (U path? string?)
  order : (U 'pre 'post) = 'pre
  filter : (path? -> boolean?) = (lambda (path) #t)
  follow-links? : boolean? = #t
A wrapper for directory-tree that returns a sequence that is compatible with for and its equivalents.

(file-pretty-size file)  string?
  file : (U path? string?)
Returns the size of file, formatted in a humane way. See perttify-file-size for examples.

(prettify-file-size size)  string?
  size : natural?
Like file-pretty-size but takes the size of the file as an integer.

Examples:

  > (prettify-file-size 500)

  "500 bytes"

  > (prettify-file-size (* 500 1024))

  "500 KB"

  > (prettify-file-size (* 1000 1024))

  "0.9 MB"

  > (prettify-file-size (* 100 1024 1024 1024))

  "100 GB"