The File Package offers a set of built-in functions for handling the OScript File data type which is used to reference and access files in the file system. The File Package facilitates file and directory creation, deletion, directory traversal, and searching, as well as the usual reading and writing functions.
A file may opened for read or write in either normal text mode, or binary mode. Files also come in two types, standard (as opened with File.Open()) and stream files (as opened with File.OpenStream()) which differ in that stream files behave as streams or pipes, and thus are not traversable (as with File.Seek() or File.Rewind()), and while the content readable from them can be exhausted for a given time, there is no "end" until the stream is closed, since, as with C's STDIN, there is always the potential for more input waiting, even if it is not present. Certain read and write functions operate on only stream files, while others operate on normal files, and some operate on both. See File.OpenStream() for further details.
The major functionalities offered in the File Package are the following:
String AppendMode
Passed to File.Write() to open the file in Append mode; sets the write pointer to the end of the file.
DOSTerminationString DOSTermination
Passed to File.LineTermination() to specify that DOS line termination be used.
E_AlreadyOpenError E_AlreadyOpen
Error returned by File.Open() indicating that the specified file is currently open.
E_CannotCopyDirsError E_CannotCopyDirs
Error returned by File.CopyStart() indicating that the specified directory is a source directory.
E_DestAlreadyExistsError E_DestAlreadyExists
Error returned by File.CopyStart() indicating that the specified destination file already exists.
E_EofError E_Eof
Error returned by File.Read() and File.CopyNext() indicating that the end of the file has been reached.
E_FailedCreatingDestError E_FailedCreatingDest
Error returned by File.CopyStart() indicating that a problem occurred while creating the destination file.
E_FailedOpeningDestError E_FailedOpeningDest
Error returned by File.CopyStart() indicating that an error occurred while attempting to open the specified destination file.
E_FailedOpeningSourceError E_FailedOpeningSource
Error returned by File.CopyStart() indicating that an error occurred while attempting to open the specified source file.
E_FileSystemErrorError E_FileSystemError
Error returned by various File built-in functions indicating that an internal File system error occurred.
E_FilenameError E_Filename
Error returned by functions that require a File specification indicating that a file name was not specified or that bad file name syntax was used.
E_InsufficientMemoryError E_InsufficientMemory
Error returned by File.CopyNext() or File.Read() indicating that there is not enough memory to perform the read or copy operation.
E_ModeError E_Mode
Error returned by File.Open() indicating that an illegal open option was specified.
E_ModeErrorError E_ModeError
Error returned by various File built-in functions indicating that the operation requires a stream File as opened with File.OpenStream() instead of a regular file as opened with File.Open(), or vice versa.
E_NotADirectoryError E_NotADirectory
Error returned by various File built-in functions indicating that the specified directory is invalid.
E_NotEnoughFreeSpaceError E_NotEnoughFreeSpace
Error returned by File.CopyStart() indicating that there is not enough disk space to place a file in the specified location.
E_NotOpenError E_NotOpen
Error returned by File.Open() indicating that the specified file is not open.
E_NotOpenForBinaryAccessError E_NotOpenForBinaryAccess
Error returned by File.ReadBytes(), File.WriteBytes(), or File.Seek() indicating that the File being operated upon is not in binary mode as required.
E_NotOpenForReadError E_NotOpenForRead
Error returned by File.Read() indicating that File.ReadMode or File.ReadBinMode was not passed as the mode parameter to File.Open().
E_NotOpenForWriteError E_NotOpenForWrite
Error returned by File.Write() indicating that File.WriteMode or File.WriteBinMode was not passed as the mode parameter to File.Open().
E_NotSameDirectoryError E_NotSameDirectory
Error returned by File.Rename() indicating that the specified directory is different than the original file directory.
E_OpenError E_Open
Error returned by File.Open() indicating that the specified file could not be opened.
E_SourceNotFoundError E_SourceNotFound
Error returned by File.CopyStart() indicating that the specified source file cannot be located.
E_TargetDirNotFoundError E_TargetDirNotFound
Error returned by File.CopyStart() indicating that the target directory cannot be located or does not exist.
E_WriteError E_Write
Error returned by File.Write() or the File.WriteBytes function indicating that there was an error writing to the file.
FileCopyTypeInteger FileCopyType
The type number for a value of type FileCopy.
FileTypeInteger FileType
The type number for a value of type File.
ReadBinModeString ReadBinMode
Passed to File.Open(); opens the file for reading in binary mode.
ReadModeString ReadMode
Passed to File.Open(); opens the file for reading in text mode.
StatFileModeString st_mode
Constant used as an index in the Assoc returned by Stat().
StatInodeNumberString st_ino
Constant used as an index in the Assoc returned by Stat().
StatDeviceIDString st_dev
Constant used as an index in the Assoc returned by Stat().
StatRDeviceString st_rdev
Constant used as an index in the Assoc returned by Stat().
StatNumberLinksString st_nlink
Constant used as an index in the Assoc returned by Stat().
StatUserIDString st_uid
Constant used as an index in the Assoc returned by Stat().
StatGroupIDString st_gid
Constant used as an index in the Assoc returned by Stat().
StatSizeString st_size
Constant used as an index in the Assoc returned by Stat().
StatLastAccessTimeString st_atime
Constant used as an index in the Assoc returned by Stat().
StatLastModifyTimeString st_mtime
Constant used as an index in the Assoc returned by Stat().
StatLastChangeTimeString st_ctime
Constant used as an index in the Assoc returned by Stat().
UNIXTerminationString UNIXTermination
Passed to File.LineTermination(); sets line termination to UNIX line termination.
WriteBinModeString WriteBinMode
Passed to File.Open(); opens the file for writing in binary mode.
WriteModeString WriteMode
Passed to File.Open(); opens the file for writing in text mode.
pCreateDateDate pCreateDate
Date and time on which the file was created.
pLineTerminationString pLineTermination
Line terminator currently set for the file. This feature defaults to the appropriate value for the platform from which the file is obtained. The File.DOSTermination and File.UNIXTermination constants are available for use with this property.
pModifyDateDate pModifyDate
The date and time on which the file was most recently modified.
pReadOnlyBoolean pReadOnly
DOS read-only file attribute.
pSizeLong pSize
The size (in bytes) of the file.
pTypeString pType
The file type.
AccessString Access( String filePath )
Searches for the specified file.
filePath | - | A String indicating the local file path and file name. The path can be specified in full or partially. The name of the file (for example, MYFILE.TXT) must be specified. |
If only a partial file path is specified, the search is performed in the following order:
Windows | Application Directory (where the Livelink resides)
Working Directory (the “Start-in” directory) %PATH% Windows Directory Windows System Directory |
UNIX | Application Directory Working Directory $PATH |
Boolean Close( File openFile )
Closes an open file. To avoid resource leaks, close all open files when they are no longer needed. See File.Open().
openFile | - | A File opened by File.Open(). |
Boolean CopyEnd( FileCopy operation )
Ends the file copy operation and closes the File being copied. See File.CopyStart() and File.CopyNext().
operation | - | A FileCopy, as returned by the File.CopyStart function, indicating the file copy operation to end. |
Boolean CopyNext( FileCopy operation )
Copies the next block in the specified file copy operation. See File.CopyStart() and File.CopyEnd().
operation | - | A FileCopy value, as returned by File.CopyStart(), indicating the file copy operation to continue. |
FileCopy CopyStart( Dynamic source, Dynamic destination, [Integer blockSize] )
Begins a file copy operation. This function fails and returns Error if the file specified by the destination parameter exists. Use File.Exists() to determine if the file exists before proceeding with File.CopyStart(). See File.CopyNext() and File.CopyEnd().
source | - | Either a File, as returned by File.StringToFile(), or a String indicating the file to be copied. |
destination | - | Either a File, as returned by File.StringToFile(), or a String indicating the path and file name of the file to be copied to the source file. |
blockSize | - | An optional Integer indicating the size, in bytes, of the file blocks to be copied. Defaults to a block size of 16K if not specified. |
Boolean Create( Dynamic newFile )
Creates an empty text file or directory. The newly created file must be opened by File.Open() before it can be used.
newFile | - | Either a file or directory, as returned by File.StringToFile(), or a String indicating the file or directory (including full path specification) to be created. Defaults to the current directory if no path is specified. A directory is created if the operating system-specific directory separator is included in newFile (for example, TEMP\). |
Boolean Delete( Dynamic delFile )
Deletes the specified file or directory. No confirmation dialog box is displayed before the specified file or directory is deleted. The File.Delete function fails and returns FALSE if the specified directory is not empty. The File.Delete function will fail if the file being deleted was opened by File.Open(). If the file is open, it must be closed by File.Close() before it can be deleted.
delFile | - | Either a File, as returned by File.StringToFile(), referencing the desired file, or a String indicating the file or directory to be deleted. Defaults to the current directory if no path is specified. |
String Dir( [String directory] )
Returns the name of the current directory and changes it if a new directory is specified.
directory | - | An optional String indicating the new current directory. If not specified, the current directory will not be changed. |
Boolean Eof( File openFile )
Determines whether the current file position is at the end of file.
openFile | - | A File opened by File.Open(). |
Boolean Exists( Dynamic theFile )
Determines if the specified file or directory exists.
theFile | - | Either a File, as returned by File.FileToString(), referencing the desired file or directory, or a String indicating the name of the file or directory. Defaults to the current directory if no path is specified. |
List FileList( [String directory] )
Returns a List of all of the files in the specified directory.
directory | - | An optional String indicating the directory whose files are to be listed. Defaults to the current directory if not specified. |
String FileToString( File theFile )
Converts a File reference to a String. This function is the converse of File.StringToFile(), and not Str.FileToString(), in that it does nothing regarding the contents of a file, but rather it only returns the file name with appropriate path information as related to an open File.
theFile | - | A File, as returned by File.StringToFile or File.Open(), referencing the File to be converted to a String. |
Dynamic Flush( File theFile )
Flushes the file buffer.
theFile | - | A File, as returned by File.StringToFile() or File.Open(). |
If the file is open for output, Flush() writes the contents of the buffer to that file. If the file is open for input, Flush() clears the contents of the buffer.
Long FreeSpace( [String target] )
Returns the number of free bytes in the target directory, or the current directory.
target | - | An optional String indicating the target directory. If not specified, the current directory will be the target. |
String GetName( Dynamic theFile )
Returns the file name of a File, not including the path.
theFile | - | Either a File, as returned by File.StringToFile() or File.Open(), referencing the desired file or a String indicating the name of the file. |
String GetPath( Dynamic myFile )
Returns the directory specification for a file.
myFile | - | Either a File, as returned by File.StringToFile() or File.Open(), referencing the desired file or a String indicating the name of the file. |
Boolean IsDir( String directory )
Determines whether the supplied String represents an existing directory.
directory | - | A String indicating the directory name. |
Boolean IsDirSpec( String directory )
Determines whether the last character of the supplied String is a directory separator.
directory | - | A String to test. |
Boolean IsText( Dynamic theFile )
Determines if the specified file is a text file.
theFile | - | Either a File, as returned by File.StringToFile() or File.Open(), referencing the desired file or a String indicating the name of the file. Defaults to the current directory if no path is specified. |
Integer LineTermination( File theFile, String terminator )
Alters the system-dependent default line termination used by File.Write() for a given File. This allows text files to be created for use on different platforms. This function alters the system-dependent default line termination used by File.Write(). This allows text files to be created for use on different platforms.
theFile | - | A File, as returned by File.StringToFile() or File.Open(), referencing the File on which to operate. |
terminator | - | A String constant indicating the type of line termination to use. |
Integer NextBlock( FileCopy operation )
Returns the ordinal number of the next file block to be copied.
operation | - | A FileCopy, as returned by File.CopyStart(), indicating the desired file copy operation. |
Integer NumBlocks( FileCopy operation )
Returns the number of file blocks to be copied.
operation | - | A FileCopy value, as returned by File.CopyStart(), indicating the desired file copy operation. |
File Open( Dynamic theFile, String mode )
Opens a text file in preparation for an append, read, or write operation. Note that the file is not opened as a stream file, as with File.OpenStream(), and certain read and write operations, such as File.ReadValue(), File.ReadUntil(), and File.WriteValue() require a stream file.
theFile | - | Either a File, as returned by File.StringToFile(), or a String indicating a file name and an optional path to the desired file. |
mode | - | A mode constant indicating the operation to be performed on the open file. |
If the file was opened for write (File.Open() mode = File.WriteMode), the write pointer is positioned at the beginning of the file, all text currently in the file is deleted. If the file was opened for append (File.Open() mode = File.AppendMode), the write pointer is positioned at the end of the file. If the specified file does not exist and either the File.AppendMode or File.WriteMode constant is specified, the file is automatically created and then opened.
The File.Open function may fail if the specified file cannot be found, if the required operating system-specific permissions are not set, or if there is insufficient memory to open the file. Each successful call to the File.Open function should be followed with a subsequent File.Close() once the file is no longer needed.
For an example, See File.Read().
File OpenStream( Dynamic theFile, String mode )
Similar to File.Open(), except that the file is opened as a stream file. A stream file differs from a regular file in that behaves as a stream, pipe, socket, or C's STDIN. Because of this cannot be traversed (File.Seek() and File.Rewind() are not supported on stream files) and, while its input can be exhausted, File.Eof() does not apply in practice, since there is no "end" to the stream since the potential for more input exists while the stream is open. Various other operations are not supported for streams, such as File.Read() or File.ReadBytes(). Meanwhile, other operations are available only to stream files, such as File.ReadUntil(), File.ReadValue(), and File.WriteValue().
theFile | - | The stream File to open, which should be either a String specifying the file, or a File as from File.FileToString(). |
mode | - | A mode constant (File.ReadMode, etc.) indicating the operation to be performed on the file. |
For an example, see File.ReadValue().
String Read( File theFile, [Integer numChars] )
Reads one line from a text file.
theFile | - | A File, as returned by File.Open(), referencing a file that was opened for a normal, and not binary, read operation. |
arg2 | - | An optional Integer indicating the number of characters to read. If not included, one line, as determined by an operating system-specific line delimiter, is read. To read a line that is longer than the default of 1024 characters, specify a value between 1024 and 65,534 inclusive. Values outside of this range will be ignored. |
The specified File must be opened by File.Open() with the mode parameter set to File.ReadMode. After execution, Read() sets the read pointer to the next character. Here is an example:
String fname = "c:/tmp/somefile.txt" File f = File.Open( fname, File.WriteMode ) File fr String s = "" File.Write( f, "This is some text." ) File.Write( f, "This is some more text." ) File.Close( f ) fr = File.Open( fname, File.ReadMode ) if ( !IsError( fr ) ) for ( s = File.Read( fr ); \ s != File.E_Eof; \ s = File.Read( fr ) ) Echo( s ) end File.Close( fr ) end
The output of the example is:
This is some text. This is some more text.
Bytes ReadBytes( File theFile, Integer numBytes )
Reads and returns a block of Bytes data from the specified File.
theFile | - | A File, opened with File.Open() in File.ReadBinMode, from which to read. |
numBytes | - | Specifies an Integer indicating the number of bytes to read, up to a maximum of 32K. |
The specified File must be opened by File.Open() with the mode parameter set to File.ReadBinMode.
String ReadUntil( File theFile, String delimiter )
Reads a stream File until the specified delimiter is found.
theFile | - | The stream File from which to read, as opened by File.OpenStream(). |
delimiter | - | A character delimiter which stops reading when encountered. |
Dynamic ReadValue( File theFile )
Reads an OScript Value from a stream File.
theFile | - | A stream file, as opened with File.OpenStream(), from which one OScript value, as written by File.WriteValue(), is read. |
File.ReadValue() can only be used on a stream. ReadValue() may return unpredictable results if the value read was not written with File.WriteValue(). Here is an example:
String fname = "c:/tmp/somefile.txt" File f = File.OpenStream( fname, File.WriteMode ) File fr String s File.WriteValue( f, "This is some text." ) File.WriteValue( f, 6 ) File.WriteValue( f, { 8, Date.Now(), 4.7, "blue" } ) File.WriteValue( f, "This is some more text." ) File.Close( f ) fr = File.OpenStream( fname, File.ReadMode ) if ( !IsError( fr ) ) while IsDefined( s = File.ReadValue( fr ) ) Echo( s ) end File.Close( fr ) end
The output of the example is:
This is some text. 6 {8,D/1998/9/2:2:7:16,G4.7,'blue'} This is some more text.
Boolean Rename( Dynamic theFile, Dynamic name )
Renames the specified file.
theFile | - | Either a File, as returned by File.StringToFile(), or a String specifying the name of the file to rename. |
name | - | Either a File, as returned by the File.StringToFile function, or a String specifying the new file name. |
A full path for the file to be renamed can be specified, but the same path must be specified in the new file name. This function cannot be used to move a file from one directory to another. This function will fail if the name parameter specifies an existing file.
Boolean Rewind( File theFile )
Sets the file read/write pointer back to the start of the file.
theFile | - | Specifies a File, as returned by File.Open(), indicating the File to rewind. |
If a File that has already been written to is rewound, the next write operation is performed at the beginning of the File, thus overwriting any previously written text.
Integer Seek( File theFile, Integer newPosition )
Set the file position pointer to a new position.
theFile | - | A File, as returned by File.Open() with mode File.ReadBinMode. |
newPosition | - | The new position that the read/write position pointer should be set to. |
String Separator()
Returns the operating system-dependent character used to separate directories in a path specification.
File SetName( File theFile, String newName )
Sets the name of a file (but not the extension).
theFile | - | A File, as returned by File.StringToFile(), of the file to be named. |
newName | - | A String indicating the new name of the file. |
String StartupDir()
Returns the full path of the Livelink start-up directory.
Assoc Stat( Dynamic statFile )
Returns the status of the specified file or directory. This is intended to be used by those who are familiar with the ANSI stat function. It will return an Assoc of values for platform-specific status information. Constants are available to use as keys into the returned Assoc (see below).
statFile | - | Either a File, as returned by File.StringToFile(), referencing the desired file, or a String indicating the file or directory. |
The valid key values are StatFileMode, StatInodeNumber, StatDeviceID, StatRDevice, StatNumberLinks, StatUserID, StatGroupID, StatSize, StatLastAccessTime, StatLastModifyTime, and StatLastChangeTime.
File StringToFile( String fileSpec )
Converts a file specification String to an OScript file reference. See File.FileToString().
fileSpec | - | Specifies a String file specification that can optionally contain path information. |
String SystemDir()
Returns the name of the System directory.
The directory returns depends upon the current platform:
Windows | Windows NT system directory |
UNIX | HOME directory of the currently running process |
String ThreadDir( [String directory] )
Sets or queries the current working directory for the this thread.
directory | - | Specifies an optional String identifying the desired working directory. Defaults to the working directory of the currently executing thread if not specified. |
Note: different threads can have different working directories.
Boolean Write( File theFile, String text )
Writes the specified text to a text file.
theFile | - | Specifies a File, as returned by either File.Open() or File.OpenStream(), referencing a file that is open for write or append in either File.WriteMode or File.WriteBinMode. |
text | - | Specifies the String to be written to the file. It can contain multiple line delimiters if multiple lines are to be written. Once written, an operating system-specific line delimiter is automatically appended to the end of the specified String. |
Integer WriteBytes( File theFile, Bytes value )
Writes the contents of Bytes to the specified file and returns the number of bytes written.
theFile | - | A File, opened with either File.Open() or File.OpenStream() with mode File.WriteBinMode, to which the Bytes are to be written. |
value | - | A Bytes value indicating the binary data to be written to the file. |
The specified File may be a stream file, but it must be opened with mode File.WriteBinMode. The function can return the error File.E_NotOpenForBinaryAccess if specified file is open but not in File.WriteBinMode, or File.E_NotOpen if specified file is not open.
Dynamic WriteValue( File theFile, Dynamic value )
Writes an OScript Value to the specified stream File.
theFile | - | A stream file, as opened with File.OpenStream() and either mode File.WriteMode or File.WriteBinMode, to which the specified value will be written. |
value | - | An OScript value which will be written to the File for later reading with File.ReadValue(). |
For an example, see File.ReadValue().