Lecture 25
Files and Folders (OS Level Imperatives)
File system operations
(require "file-operations.rkt")
Path names�and tree-structured file systems
Tree-structured File Systems
root
a
b
d
e
f
g
c
File paths
root
a
b
d
e
f
g
c
root -> b -> f
Specifying files using pathnames
/Users/connor/Desktop
The pathname of my Desktop on my Mac
C:\Users\connor\Desktop
The pathname the same folder on my PC
C:\Windows\sys32\virus.exe
A file called “virus.exe” inside the sys32 folder inside the Windows folder.
D:\foo\bar\baz.txt
A text file on the D drive of my PC
Files, directories, and their paths
/
/a
/ b
/a/d
/a/e
/b/f
/b/g
/c
root
a
b
d
e
f
g
c
Delimiters in pathnames
>foo>bar>baz.txt
/foo/bar/baz.txt
C:/foo/bar/baz.txt
Windows pathname delimiters
Mac pathname delimiters
Linux pathname delimiters
Path objects in Racket
/Users/connor/Desktop
#<path:/Users/ian/Desktop>
C:\Users\connor\Desktop
#<path:C:\Users\connor\Desktop>
C:\Windows\sys32\virus.exe
#<path:C:\Windows\sys32\virus.exe>
D:\foo\bar\baz.txt
#<path:D:\foo\bar\baz>
Creating a Path in DrRacket
; build-path : string ... -> Path
; builds a path from a series of strings
> (build-path "test" "test_2" "bar.txt")
/test/test_2/bar.txt
> (build-path "test" "test_3")
/test/test_3/
Getting JUST the filename from a Path
(path-filename path)
Returns just the filename (no directories etc.) from a path
Converting Paths to/from Strings
(define p (build-path "test" "test_3"))
> (path->string p)
"test/test_3"
> (string->path "test/test_2/bar.txt")
#<path:test/test_2/bar.txt>
Walking the file system
Finding all the files in a subtree
Finding all files in a subtree
(define (all-files directory)
(append (directory-files directory)
(apply append
(map all-files
(directory-subdirectories
directory)))))