File I/O This section describes the commands and functions that can manipulate data files.
Command Summary:
Open var, mode, pathname$
Close FileID
Create pathname$, creator, filetype
CreateFolder pathname$
Delete pathname$
Rename oldPathname$, newName$
CopyFile srcFilename$, destFilename$
Lock pathname$
Unlock pathname$
Write FileID, text$
WriteBuffer FileID, BufferID, StartLocation, NumberOfBytes
ReadBuffer FileID, BufferID, StartLocation, NumberOfBytes
Seek FileID, locationFunction Summary:
var$ = OpenDialog ( fileTypes$ )
var$ = SaveDialog ( pathname$, prompt$ )
var$ = Read ( FileID, NumberOf Bytes )
var$ = LineInput ( FileID )
var = ReadBuffer( FileID, BufferID, StartLocation, NumberOfChars )
var = LOF ( FileID )
var$ = Dir ( pathname$ )
var = NumFiles ( pathname$ )
var$ = GetFilename ( pathname$, file# )
var = EOF ( FileID )
var = LOC ( FileID )
var = FileExist ( pathname$ )
var = FileName ( pathname$ )
var = FileSize ( pathname$ )
var = TypeCvt ( fileType$ )
Open var, mode, pathname$
This command will open a file for read and write operations. You can use the OpenDialog( ) and SaveDialog( ) functions to have the user select a filename.
var - This should be a numeric variable that will contain a value the should be used to reference the file being opened. This variable will equal a -1 if an error occurs.
mode - This is a numeric expression designating options used when opening the designated file.
File Open Options
1 - Read Exclusive Permission
2 - Write Exclusive Permission
3 - Exclusive Read/Write Permission
4 - Shared Read/Write Permission
.
pathname$ - This should be a string expression
that contains the name of the file that is to be opened. If the file is
not located in the current default directory then you must specify the
directory path specification.
For example:
Harddisk:MyFolder:MyFile
.
Close FileID
This command will close a file that was previously open with the OPEN command.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
.
Create pathname$, creator, filetype
This command will create a new empty file. If there is an existing file with the same pathname then it will delete it and create a new empty file.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
creator - This should be a numeric value used to indicate the creator application of the file.
filetype - This should be a numeric value used to indicate the type of file to be created.
.
Use the TypeCvt() function to help in converting text in to a numeric format. The following is an example of creating a empty text file that when clicked on will launch TeachText or SimpleText:
Example:
Create "TextFile", TypeCvt( "ttxt"), TypeCvt( "TEXT")
CreateFolder pathname$
This command will create a new folder.
pathname$ - This should be a string expression that contains the name of the folder that is to be created.
For example: Harddisk:MyNewFolder
Delete pathname$
This command will delete an existing unopened file or folder.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification.
For example:Harddisk:MyFolder:MyFile
Rename oldPathname$,newName$
This command will rename an existing unopened file or a folder .
oldPathname$ - This should be a string containing the pathname of the file/folder to be renamed. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
newFilename$ - This should be a string expression containing the new file/folder name. It should not be a pathname, just a file/folder name.
CopyFile Pathname1$, Pathname2$
This command will copy a file. Both the data and resource forks of the file will be copied.
Pathname1$ - This should be a string containing the pathname of the file to be copied. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
Pathname2$ - This should be a string containing the pathname of the new file tthat will be created and made a duplicate of srcFileName. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:NewFile
.
Lock pathname$
This command will lock an existing file.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
.
Unlock pathname$
This command will unlock an existing file.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
.
Write FileID, text$
This command will write data from the text$ expression to a file.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
text$ - This should be a string value that will be written to the specified open file.
.
WriteBuffer FileID, BufferID, StartLocation, NumberOfChars
This command will write data from a buffer to a file.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
BufferID - This should be a numeric value used to reference a memory buffer. This is usually the variable used in the MemAlloc command.
StartLocation - This should be a numeric value used to specify the location of the buffers starting byte number.
NumberOfChars - This should be a numeric value that specified the number of characters (or bytes) that will be written.
.
Seek FileID, location
This command will move the file position pointer.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
location - This should be a numeric value used to specify the location in the file to move to.
var$ = OpenDialog( fileTypes$ )
This function will allow the user to select a filename from the standard Open File Dialog for later file operations. This function returns a pathname it does not open the file.
filetypes$ - This should be a string expression that contains the types of files that should be displayed in the Open File Dialog. Each type should be 4 characters long (ie.. TEXT) If the string is empty (equals "") then all filetypes will be displayed.
Example: Dim a$ ' Filter for only 'TEXT' and 'rsrc' file types a$ = OpenDialog( "TEXTrsrc" )
var$ = SaveDialog( pathname$, prompt$ )
This function will allow the user to select or enter a filename in the Save File Dialog for later save operations. Once the user selects the SAVE button in the dialog the string variable var$ will contain the pathname. This variable will be empty (equals "") if the user canceled and no file was selected.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
prompt$ - This should be a text string value for the prompt in the Save File Dialog requesting the user to enter a filename.
var$ = Read( FileID, NumberOfChars )
This function will return a string containing data read from an open file.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
NumberOfBytes - This should be a numeric value that specified the number of bytes that will be read.
var$ = LineInput( FileID )
This function will return a string containing the next line (to the next carriage return (CR) , linefeed(LF), or up to 300 characters) read from an open file.
This function is for reading text lines from a file. It will not include the carriage return or linefeed characters, these characters will be skipped for reading of the next line with this function. If 300 characters are read without a carriage return and/or linefeed then a string containing the 300 characters will be returned.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
var = ReadBuffer( FileID, BufferID, StartLocation, NumberOfChars )
This function will read from an open into a buffer. The value that this function returns is the number of bytes actually read from the file.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
BufferID - This should be a numeric value used to reference a memory buffer. This is usually the variable used in the MemAlloc command.
StartLocation - This should be a numeric value used to specify the location of the buffers starting byte number.
NumberOfChars - This should be a numeric value that specified the number of characters (or bytes) that will be read.
var = LOF( FileID )
This function will return a numeric value indicating the length of a file opened with the Open command.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
var = EOF( FileID )
This function will return a numeric value indicating whether the file position pointer is at the end of the file.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
var = LOC( FileID )
This function will return the file position pointer of a file opened with the Open command.
FileID - This should be a numeric value used to reference an open file. This is usually the variable used in the Open command.
var$ = Dir( pathname$ )
This function will return a string value containing the file & folder names from the specified folder. Use the NumFiles and GetFileName functions to retrieve information from the returned string value. This string may contain up to 500 file/folder names. Please note that if you store this value in a string variable, that string variable must be allocated 32000 bytes to avoid possible truncation of the string.
pathname$ - This should be a string expression that contains the name of the folder that is to be used.
Examples: Dim D$[32000], CTR, F$ D$ = Dir("Harddisk:MyFolder") Print "Number of Files=",Numfiles(D$) For CTR=1 to Numfiles(D$)Print GetFilename( D$, CTR)Next CTR 'another way CTR = NumFiles( Dir("Harddisk:MyFolder"))
var = NumFiles( dirString$ )
This function will return the number of files/folders in the dirString$, returned from the Dir( ) function.
dirString$ - This should be a string value returned from the Dir( ) function.
var$ = GetFilename( dirString$, file# )
This function will return a file/folder name from the dirString$, returned from the Dir( ) function.
dirString$ - This should be a string value returned from the Dir( ) function.
file# - This should be a numeric value to specify the which file in the dirString to return. It should less or equal to the value return from the NumFiles( ) function..
var = FileExist( pathname$ )
This function will return a value indicating whether a file exists.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
var = FileSize( pathname$ )
This function will return a value indicating the length of an unopened file. This value includes both the Data and Resource forks of the file. The LOF() function only returns the size of the Data fork.
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
var$ = FileName( pathname$ )
This function will return text string that is just the filename part of a pathname$. In the example below it woulfd return just "MyFile".
pathname$ - This should be a string expression that contains the name of the file that is to be used. If the file is not located in the current default directory then you must specify the directory path specification. For example:
Harddisk:MyFolder:MyFile
var =TypeCvt( text$ )
This function will return a value that converted from a 4 character long text string that represents a creator or filetype code. See the Create command for an example of this function.
text$ - This should be a string expression that contains a 4 character long text string that represents a creator or filetype code.