On this page:
dir
file
create-dir
date

1.12 Working with Files and Directories: "dir.rkt" 🔗

 (require htdp/dir) package: htdp-lib

The teachpack provides structures and functions for working with files and directories:

struct

(struct dir (name dirs files)
    #:extra-constructor-name make-dir)
  name : (or/c string? symbol?)
  dirs : (listof dir?)
  files : (listof file?)
Represents directories (file folders) in the teaching languages.

struct

(struct file (name size date content)
    #:extra-constructor-name make-file)
  name : (or/c string? symbol?)
  size : integer?
  date : (or/c 0 date?)
  content : any/c
Represents files in the teaching languages. The struct’s date field is optional for clients. Calling make-field with three arguments fills the time field with 0.

procedure

(create-dir path)  dir?

  path : string?
Turns the directory found at path on your computer into an instance of dir.

struct

(struct date (year month day hours minutes seconds)
    #:extra-constructor-name make-date)
  year : natural-number/c
  month : natural-number/c
  day : natural-number/c
  hours : natural-number/c
  minutes : natural-number/c
  seconds : natural-number/c
Represents dates for file construction.

Sample: Set teachpack to "dir.rkt" or add (require htdp/dir) to the definitions area. Clicking on Run and asking for the content of the current directory will produce something like this:
> (create-dir ".")
(make-dir
  "."
  '()
  (cons (make-file "arrow.scrbl" 1897 (make-date 15 1 15 11 22 21) "")
    (cons (make-file "convert.scrbl" 2071 (make-date 15 1 15 11 22 21) "")
      (cons (make-file "dir.scrbl" 1587 (make-date 8 7 8 9 23 52) "")
        (cons (make-file "docs.scrbl" 1259 (make-date 15 1 15 11 22 21) "")
          (cons (make-file "draw.scrbl" 5220 (make-date 15 1 15 11 22 21) "")
            (cons (make-file "elevator.scrbl" 1110 (make-date 15 1 15 11 22 21) ""))))))))
Using "." usually means the directory in which your program is located. In this case, the directory contains no sub-directories and six files.

Note The library generates file names as strings, but the constructors accept symbols for backwards compatibility.

Note Soft links are always treated as if they were empty files.

Changed in version 1.0 of package htdp-lib: built in 1996 for HtDP/1e
Changed in version 1.4: Fri Jul 8 13:09:13 EDT 2016 added optional date field to file representation, added strings as representations of file names