Programmer's Reference

Copying files

A file can be copied by sending the copy:new: message to the CfsFileDescriptor class. An example is shown below.

CfsFileDescriptor copy: 'filename.txt' new: 'filename.bak'.
Tip:On platforms which do not provide an OS file copy function, copy:new: is performed using buffered read/write.

Copy can be performed across devices. An example of an efficient move function implemented using rename and copy is shown below. For an explanation of errno codes, see "Handling errors".

|  result pathA pathB |
"Attempt to move the from pathA to pathB using rename.
If that fails because it cannot rename across devices,
copy it and delete the original."
pathA := 'C:\temp\test.txt'.
pathB := 'A:\test.new'.
result := CfsFileDescriptor rename: pathA new: pathB.
(result isCfsError and: [result errno = EXDEV] )
    ifTrue: [
         "Platform cannot rename across devices"
         result := CfsFileDescriptor copy: pathA new: pathB.
         result isCfsError
             ifFalse: [
                  "Copy successful, delete original"
                  result := CfsFileDescriptor remove: pathA.
             ].
   ].
^result


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]