On this page:
A File System (100 minutes)
Before you go...
6.10

Lab 9 Files and Directories

home work!

Purpose: The purpose of this lab is to practice working with tree-structured data.

Textbook references: Chapter 19: The Poetry of S-Expressions, Chapter 20: Incremental Refinement

A File System (100 minutes)

Goals: Practice working with tree-structured data.

; A File is a (make-file String Integer Date String)
(define-struct file [name size date content])
; - where name is the name of the file (including extension)
; - size is the size of the file in bytes
; - date is the last modified date of the file
; - and content is the contents of the file
 
; A Date is a (make-date Integer Integer Integer Integer Integer Integer)
(define-struct date [year month day hours minutes seconds])
; - where year is the year the file was modified
; - month is the month the file was modified
; - day is the day the file was modified
; - hours is the hour the file was modified
; - minutes is the minute the file was modified
; - and seconds is the second the file was modified
 
; A Directory is a (make-dir Symbol [List-of Directory] [List-of File])
(define-struct dir [name dirs files])
; - where name is the directory path
; - dirs is the list of sub-directories in this directory
; - and files is the list of files in this directory
;   (not including the ones in sub-directories)
 
; DO NOT COPY THE ABOVE STRUCTURE DEFINITIONS!
; They are provided for you in htdp/dir

Sample Problem Define some example directories using the files and directories on your computer. You’ll need to use (require htdp/dir) in order to do this. You may notice that the content field for the files is always an empty string. This is because actually getting all the contents of your files would take a lot of memory and we want to do fun things with this data instead of waiting for it to get all the contents of all your files.

(define CAREER-DIR (create-dir "C:\\Users\\rolob\\Documents\\Career\\"))
(define FUNDIES-DIR (create-dir "C:\\Users\\rolob\\Documents\\Fundies\\"))

Sample Problem Design a function that consumes a Directory and produces the total number of files in it, however deeply they might be nested inside directories.

; num-files : Directory -> Number
; Produces the total number of files in the directory
(check-expect (num-files (make-dir 'Empty '() '())) 0)
(check-expect
 (num-files
  (make-dir
   'Career
   (list (make-dir 'CareerApplications
                   '()
                   (list (make-file "CoverLetter.doc" 31744
                                    (make-date 2015 9 20 11 36 25) "")
                         (make-file "EmploymentApplication.pdf" 231010
                                    (make-date 2015 10 13 13 10 0) "")))
         (make-dir 'CareerMyJob
                   '()
                   (list (make-file "BackgroundCheck.pdf" 1040138
                                    (make-date 2016 8 23 10 27 10) "")
                         (make-file "I9.pdf" 963654
                                    (make-date 2015 11 20 15 49 45) "")
                         (make-file "JobOffer.pdf" 507887
                                    (make-date 2015 11 20 15 49 0) ""))))
   (list (make-file "References.docx" 11634
                    (make-date 2016 8 6 9 55 15) "")
         (make-file "Resume.doc" 34816
                    (make-date 2016 10 12 13 18 12) "")
         (make-file "Transcript.doc" 140288
                    (make-date 2015 9 11 9 3 0) "")))) 8)
(define (num-files d)
  (local [; Directory Number -> Number
          ; Add number of files from this subdirectory to total so far
          (define (add-subd subd sofar)
            (+ (num-files subd) sofar))]
  (+ (foldr add-subd 0 (dir-dirs d))
     (length (dir-files d)))))

Exercise 1 Design a function that consumes a Directory and a String representing the name of the file and returns true if there is a file with that name in the directory or any of its subdirectories. Otherwise, it returns #false.

Exercise 2 Design a function that consumes a Directory d and computes the total size of all files in the whole directory tree. You may assume that directories take up no space.

Exercise 3 Design a function that consumes a Directory d and two Strings src and target, and produces a Directory which has the same files as d but with all files named src renamed to target.

Exercise 4 Design a function that consumes a Directory d and a Number n representing file size and returns a list of files in the directory tree of d with size at least n.

Exercise 5 Design a function that draws a file as an image. The image has to show the name of the file and its size in kilobytes (note that there are 1024 bytes in a kilobyte).

Exercise 6 Design a function that draws a directory. The image has to show the name of the directory and the number of files in the directory, as well as showing all the folders and files in the directory tree. An example of what this should look like is given below.

Hint: Your previous function could come in handy!

Before you go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.