► |  | How to Design Programs Teachpacks

1 | Ht DP Teachpacks
2 | Ht DP/ 2e Teachpacks

▼ | 1 | Ht DP Teachpacks

1.1 | Manipulating Images: "image.rkt"
1.2 | Simulations and Animations: "world.rkt"
1.3 | Converting Temperatures: "convert.rkt"
1.4 | Guessing Numbers: "guess.rkt"
1.5 | Master Minding: "master.rkt"
1.6 | Playing Master Mind: "master-play.rkt"
1.7 | Simple Drawing: "draw.rkt"
1.8 | Hangman: "hangman.rkt"
1.9 | Playing Hangman: "hangman-play.rkt"
1.10 | Managing Control Arrows: "arrow.rkt"
1.11 | Manipulating Simple HTML Documents: "docs.rkt"
1.12 | Working with Files and Directories: "dir.rkt"
1.13 | Graphing Functions: "graphing.rkt"
1.14 | Simple Graphical User Interfaces: "gui.rkt"
1.15 | An Arrow GUI: "arrow-gui.rkt"
1.16 | Controlling an Elevator: "elevator.rkt"
1.17 | Lookup GUI: "lkup-gui.rkt"
1.18 | Guess GUI: "guess-gui.rkt"
1.19 | Queens: "show-queen.rkt"
1.20 | Matrix Functions: "matrix.rkt"

On this page:

dir
file
create-dir
date

top ← prev up next →

#### 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

top ← prev up next →