2.4: Renaming, Copying and Deleting Files and Directories
- Page ID
- 43795
Files may be copied and renamed using the messages copyTo:
and renameTo:
. Note that while copyTo:
tasks as argument another fileReference
, renameTo:
takes a path, pathname or reference.
| working | working := FileSystem disk workingDirectory. working / 'foo.txt' writeStreamDo: [ :stream | stream nextPutAll: 'Hello World' ]. working / 'foo.txt' copyTo: (working / 'bar.txt'). | working | working := FileSystem disk workingDirectory. working / 'bar.txt' readStreamDo: [ :stream | stream contents ]. → 'Hello World' | working | working := FileSystem disk workingDirectory. working / 'foo.txt' renameTo: 'skweek.txt'. | working | working := FileSystem disk workingDirectory. working / 'skweek.txt' readStreamDo: [ :stream | stream contents ]. → 'Hello World'
Directory creation. To create a directory, use the message createDirectory
as follows:
| working | working := FileSystem disk workingDirectory. backup := working / 'cache-backup'. backup createDirectory. backup isDirectory. → true backup children. → #()
Copy everything. You can copy the contents of a directory using the message copyAllTo:
. Here we copy the complete package-cache to the backup directory using copyAllTo:
:
cache copyAllTo: backup.
Note that before copying the target directory is created if it does not exist.
Deleting. To delete a single file, use the message delete
:
(working / 'bar.txt') delete.
To delete a complete directory tree (including the receiver) use deleteAll
. Be careful to not delete the wrong folder though.
backup deleteAll.