ScriptBasic > Tutorials

Working with files

(1/1)

Support:
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: ---OPEN file_name FOR access_mode AS [#]file_number [LEN=record_length]

--- End code ---

Access Modes:

INPUT
OUTPUT
APPEND
RANDOM
BINARY

You can switch modes for a file while it's open with the following directives.


--- Code: ---BINMODE [#]file_number

TEXTMODE [#]file_number

--- End code ---

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


--- Code: ---BINMODE INPUT
BINMODE OUTPUT

TEXTMODE INPUT
TEXTMODE OUTPUT

--- End code ---

The FREEFILE function returns an available file number.


--- Code: ---file_number = FREEFILE

--- End code ---

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: ---SEEK [#]file_number, position
--- End code ---

To return to the beginning of the file, use the SEEK or REWIND directives.


--- Code: ---SEEK [#]file_number, 0
REWIND [#]file_number

--- End code ---

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: ---number_of_records = LOF(file_number)
--- End code ---

The POS function returns the current record position if the LEN=record_lenght is used otherwise the current byte position is returned.


--- Code: ---record_position = POS(file_number)
--- End code ---

To write to a file, the PRINT directive is used.


--- Code: ---PRINT #file_number,expression_list
--- End code ---

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: ---PRINT #file_numebr,"some test to write\n"
PRINTNL #file_numebr,"some test to write"

--- End code ---

To read lines from a opened text file, the LINE INPUT directive is used.


--- Code: ---LINE INPUT #file_number,variable
--- End code ---

To read from a binary file use the INPUT function. The function will return the number of bytes specified or the remaining data.


--- Code: ---variable = INPUT(number_of_bytes,file_number)
--- End code ---

The current working directory can be determined with the CURDIR system variable and changed with the CHDIR directive.


--- Code: ---PRINT CURDIR

CHDIR "directory_path"

--- End code ---

ScriptBasic supports advisory locking of files.


--- Code: ---LOCK #file_number,option
--- End code ---

Options:

WRITE
READ
RELEASE

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


--- Code: ---LOCK RANGE #file_number FROM start_pos TO end_pos FOR option
--- End code ---

Options are the same as the LOCK directive.

An open binary file my be truncated with the TRUNCATE directive.


--- Code: ---TRUNCATE #file_number,new_file_size
--- End code ---

A file or empty directory can be removed with the DELETE directive.


--- Code: ---DELETE "file_name"
DELETE "directory_name"

--- End code ---

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: ---MKDIR "dir_name"
MKDIR "dir_name/sub_dir_name"

--- End code ---

Returns true if the named file exists.


--- Code: ---status = FILEEXISTS(file_name)
--- End code ---


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


--- Code: ---file_size = FILELEN("file_name")
--- End code ---


File parameters can be set using the SET FILE directive.


--- Code: ---SET FILE file_name parameter=value
--- End code ---

Parameters:

Owner
CreateTime
ModifyTime
AccessTime


Get the time the file was modified last time.


--- Code: ---fct = FILECREATETIME(file_name)
--- End code ---


Get the time the file was accessed last time.


--- Code: ---fat = FILEACCESSTIME(file_name)
--- End code ---


Get the time the file was modified last time.


--- Code: ---fmt = FILEMODIFYTIME(file_name)
--- End code ---


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: ---FILECOPY file_name_from, file_name_to
--- End code ---

Navigation

[0] Message Index

Go to full version