Chapter 7: File Routines


Overview

This chapter introduces the subroutines and functions supported by OpenOffice.org Basic that are related to files and directories. After reading this chapter you'll be able to create, delete, rename, and move files and directories. You'll learn methods that inspect files, both open and closed, and directories. This chapter also explains the idiosyncrasies and bugs related to reading and writing files, along with differences between operating systems.

OOo Basic includes functions that allow you to interact with the file system (see Table 1 ). You can perform simple and complex tasks such as creating and deleting directories, or even opening and parsing files. In this chapter I'll spend a fair amount of time on directories, file attributes, and the different file types. I will examine how files are organized and manipulated, how the different file types are structured, and which functions read and write data for those different file types. I was happy with how easily I was able to write macros to move and rename files. On the other hand, the functions to manipulate binary and random files feel rough around the edges.

Table 1: File functions in OOo Basic.

Function

Description

ChDir( path )

Change the currently logged directory or drive. Deprecated; do not use.

ChDrive(path)

Change the currently logged drive. Deprecated; do not use.

Close #n

Close a previously opened file or files. Separate file numbers with a comma.

ConvertFromURL(str)

Convert a path expressed as a URL to a system-specific path.

ConvertToURL(str)

Convert a system-specific path to a URL.

CurDir CurDir(drive)

Return the current directory as a system-specific path. If the optional drive is specified, the current directory for the specified drive is returned.

Dir(path)

Dir(path, attr)

Return a listing of files based on an included path. The path may contain a file specification-for example, "/home/andy/*.txt". Optional attributes determine if a listing of files or directories is returned.

EOF(number)

Return True if the file denoted by "number" is at the end of the file.

FileAttr(number, 1)

Return the mode used to open the file given by "number". The second argument specifies if the file-access or the operating-system mode is desired, but only the file mode is currently supported.

FileCopy(src, dest)

Copy a file from source to destination.

FileDateTime(path)

Return the date and time of the specified file as a string.

FileExists(path)

Return True if the specified file or directory exists.

FileLen(path)

Return the length of the specified file as a long.

FreeFile()

Return the next available file number for use.

Get #number, variable

Get #number, pos, variable

Read a record from a relative file, or a sequence of bytes from a binary file, into a variable. If the position argument is omitted, data is read from the current position in the file. For files opened in binary mode, the position is the byte position in the file.

GetAttr(path)

Return a bit pattern identifying the file type.

GetPathSeparator()

Return the system-specific path separator.

Input #number, var

Sequentially read numeric or string records from an open file and assign the data to one or more variables . The carriage return (ASC=13), line feed (ASC=10), and comma act as delimiters. Input cannot read commas or quotation marks (") because they delimit the text. Use the Line Input statement if you must do this.

Kill(path)

Delete a file from disk.

Line Input #number, var

Sequentially read strings to a variable line-by-line up to the first carriage return (ASC=13) or line feed (ASC=10). Line end marks are not returned.

Loc(number)

Return the current position in an open file.

LOF(number)

Return the size of an open file, in bytes.

MkDir(path)

Create the directory.

Name src As dest

Rename a file or directory.

Open path For Input As #n

Open a data channel (file) for reading or writing.

Put #n, var Put #n, pos, var

Write a record to a relative file or a sequence of bytes to a binary file.

Reset

Close all open files and flush all files to disk.

RmDir(path)

Remove a directory.

Seek #n, pos

Set the position for the next writing or reading in a file.

SetAttr(path, attr)

Set file attributes.

Write #n, string

Write data to a file.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net