Author Topic: Working with files  (Read 14731 times)

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
Working with files
« on: September 24, 2008, 05:51:58 PM »
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.

Code: [Select]
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.

Code: [Select]
BINMODE [#]file_number

TEXTMODE [#]file_number

To change ScriptBasic's standard IN/OUT modes, these directives can be used.

Code: [Select]
BINMODE INPUT
BINMODE OUTPUT

TEXTMODE INPUT
TEXTMODE OUTPUT

The FREEFILE function returns an available file number.

Code: [Select]
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.

Code: [Select]
SEEK [#]file_number, position
To return to the beginning of the file, use the SEEK or REWIND directives.

Code: [Select]
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.

Code: [Select]
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.

Code: [Select]
record_position = POS(file_number)
To write to a file, the PRINT directive is used.

Code: [Select]
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.

Code: [Select]
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.

Code: [Select]
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.

Code: [Select]
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.

Code: [Select]
PRINT CURDIR

CHDIR "directory_path"

ScriptBasic supports advisory locking of files.

Code: [Select]
LOCK #file_number,option
Options:

WRITE
READ
RELEASE

A range of bytes within a file may be locked as well.

Code: [Select]
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.

Code: [Select]
TRUNCATE #file_number,new_file_size
A file or empty directory can be removed with the DELETE directive.

Code: [Select]
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.

Code: [Select]
MKDIR "dir_name"
MKDIR "dir_name/sub_dir_name"

Returns true if the named file exists.

Code: [Select]
status = FILEEXISTS(file_name)

The FILELEN function returns the length in bytes of an unopened file given it's filename as the argument.

Code: [Select]
file_size = FILELEN("file_name")

File parameters can be set using the SET FILE directive.

Code: [Select]
SET FILE file_name parameter=value
Parameters:

Owner
CreateTime
ModifyTime
AccessTime


Get the time the file was modified last time.

Code: [Select]
fct = FILECREATETIME(file_name)

Get the time the file was accessed last time.

Code: [Select]
fat = FILEACCESSTIME(file_name)

Get the time the file was modified last time.

Code: [Select]
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.

Code: [Select]
FILECOPY file_name_from, file_name_to