Answer an instance of me representing the current directory.
dlrectoryFromName: fileName setFlleName: aBlock
Answer the file directory implied from the designator fileName. This directory contains the designated file. Evaluate the block with only the file name portion of the designator.
directoryNamed: aString
Answer an instance of me whose name is aString. Answer Disk if aString matches Disk.
fileNamed: aFileName
Not appropriate for a FileDirectory.
Rationale
FileDireclory is a representation of a directory on the disk. It provides protocol for looking at the files in a directory.
Discussion
Disk is a global variable, an instance of File Directory. In the standard image Disk is set to ".", the current directory. This means that Disk "floats" to whatever directory you are in when you invoke Smalltalk. The Smalltalk home directory
FileDirectory OS-Streams
code in the System Workspace for changing the value of Disk.
Class Protocol
Instance creation
provides several ways to create an instance of FlleDlrectory.Instances can be created from the current directory, from the directory implied in a full path name, or by naming a directory. The inherited fIIeNamed: method is intercepted.
Instance Protocol
Accessing
methods answer the directory name, answer a collection of the file names in the receiver, answer the name instance variable, and answer false to indicate that version numbers are not supported. Complete path means begin at root and fully specify the path. Full name means the name relative to Disk if Disk is part of the path, or the complete path relative to root (I) if Disk is not in the file's path.Adding
has one method which creates a new file with the specified name.Enumerating
has two methods which evaluate a block for all entries in the directory and a method, fllesMatchlng:, that returns an array of file names matching an ambiguous file name - the ambiguous name provides the ability to use wildcard characters such as # or*.
File accessing
methods perform many types of functions. You can check for invalid characters (Jess than ASCII 33 or greater than ASCII 126) in a file name and, optionally, remove the illegal characters. Two methods return true if a specified file name does not contain illegal characters; in addition, one of them checks that the file exists before answering true. One method answers the appropriate class of Smalltalk objects representing files in these directory objects - this allows some independence for File Directory.Severalfile accessing
methods return instances of this class and FileStream. You can choose among methods that check whether a file exists before creating a stream on it, that further specifies that the stream be write-only, or one that creates the stream without testing for the file's existence. This message category also has methods to create a new file or directory and rename an existing file.If you don't want to always fully qualify the path of a file (or files) that you access frequently, you can use the following method.
• Create an instance of File Directory on the directory which contains the file(s).
Keep the instance of FileDirectory in your image.
FileDirectory OS-Streams
• Send a message to the FileDlrectory to create a FileStream on the file when you want to access it (e.g., aFileDirectory oldFile: nameOfFile). The
FlieStream will be created relative to the path of the FileDlrectory.
The above approach can be used for any directories you access often - just keep the instances of File Directory in your image. .
File copying
contains one method which appends the contents of a specified file to another file and one method which copies the contents of one file to another file.Removing
methods let you remove a disk file from the directory and, optionally, specify a block to be evaluated if the disk file does not exist.Testing
methods check whether a disk file exists, whether the directory is empty, and answer an instance of the file status class for your operating system.The single
xerox/de compatibility
method, flndKey: calls fileClass and returns an instance of the class that method specifies. This method is retained for backward compatibility.Examples
The following example can be executed in a workspace. If you do so, you might want to change the ambiguous file name to yield files you actually want to file in.
For ease of use, a file list is better for filing in one file at a time. If you want a group of files to file in, the example code is a fast way to do it.
dir ~ Disk directoryNamed: '/usr/lib/smalltalkJfileln'.
(dir filesMatching: 'a*.st') do:
[:eachFile
I
(dir file: eachFile) filelruThe global FlleDlrectory, Disk, is sent an
instance creation
message with the full path name as an argument. The instance is assigned todir.
Anenumerating
message, filesMatching:, is sent todir
with an ambiguous file name as the argument. For each file whose name begins witha
and has the extension .st, an instance of FileStream is created. A FileDlrectoryfile accessing
message, file:, creates the instances. Each instance is sent the message fileln, which incorporates the contents of the file into the image.Related Classes
You are probably familiar with the results of some of this class' protocol, because FileDirectory is used in File Lists. Look in your System Workspace under "Create