Q. What types of files does ScriptBasic support?
A. ScriptBasic supports text and binary files native and a verity of database options with extension modules.
A file is just a byte stream to ScriptBasic. You can open a file for reading, writing, or both of these operations without closing and reopening the file.
OPEN file_name FOR access_mode AS [#]file_number [LEN=record_length]
Access Modes:
INPUT
OUTPUT
APPEND
RANDOM
BINARY
You can switch modes for a file while it's open with the following directives.
BINMODE [#]file_number
TEXTMODE [#]file_number
To change ScriptBasic's standard IN/OUT modes, these directives can be used.
BINMODE INPUT
BINMODE OUTPUT
TEXTMODE INPUT
TEXTMODE OUTPUT
The FREEFILE function returns an available file number.
file_number = FREEFILE
The ISDEFINED(file_numeber) or ON ERROR GOTO can be used to determine if a file handle was return by FREEFILE().
When a file is opened in RANDOM or BINARY modes, the SEEK directive may be used to position the file pointer for the next read or write operation.
SEEK [#]file_number, position
To return to the beginning of the file, use the SEEK or REWIND directives.
SEEK [#]file_number, 0
REWIND [#]file_number
If the file is opened using the optional LEN=record_length option, then the LOF function returns the total number of records for the file otherwise the LOF function returns the total number of bytes for the file.
number_of_records = LOF(file_number)
The POS function returns the current record position if the LEN=record_lenght is used otherwise the current byte position is returned.
record_position = POS(file_number)
To write to a file, the PRINT directive is used.
PRINT #file_number,expression_list
If the file is opened in text mode and a new line character is required at the end of the line, the following syntax can be used.
PRINT #file_numebr,"some test to write\n"
PRINTNL #file_numebr,"some test to write"
To read lines from a opened text file, the LINE INPUT directive is used.
LINE INPUT #file_number,variable
To read from a binary file use the INPUT function. The function will return the number of bytes specified or the remaining data.
variable = INPUT(number_of_bytes,file_number)
The current working directory can be determined with the CURDIR system variable and changed with the CHDIR directive.
PRINT CURDIR
CHDIR "directory_path"
ScriptBasic supports advisory locking of files.
LOCK #file_number,option
Options:
WRITE
READ
RELEASE
A range of bytes within a file may be locked as well.
LOCK RANGE #file_number FROM start_pos TO end_pos FOR option
Options are the same as the LOCK directive.
An open binary file my be truncated with the TRUNCATE directive.
TRUNCATE #file_number,new_file_size
A file or empty directory can be removed with the DELETE directive.
DELETE "file_name"
DELETE "directory_name"
To remove a directory and everything below it, the DELTREE directive can be used. Be careful with this command as ALL files/directories below the given directory name will be permanently deleted. (no recycle bin use)
To create a directory or a new path made up of multiple sub-directories use the MKDIR directive.
MKDIR "dir_name"
MKDIR "dir_name/sub_dir_name"
Returns true if the named file exists.
status = FILEEXISTS(file_name)
The FILELEN function returns the length in bytes of an unopened file given it's filename as the argument.
file_size = FILELEN("file_name")
File parameters can be set using the SET FILE directive.
SET FILE file_name parameter=value
Parameters:
Owner
CreateTime
ModifyTime
AccessTime
Get the time the file was modified last time.
fct = FILECREATETIME(file_name)
Get the time the file was accessed last time.
fat = FILEACCESSTIME(file_name)
Get the time the file was modified last time.
fmt = FILEMODIFYTIME(file_name)
If the destination file already exists then the command overwrites the file. If the destination file is to be created in a directory that does not exist yet then the directory is created automatically.
FILECOPY file_name_from, file_name_to