ScriptBasic
February 09, 2012, 07:12:20 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: The ScriptBasic User, Developer and extension module guides are available at the WIKI.
 
   Home   Wiki Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Working with files  (Read 1236 times)
support
Administrator
*****
Posts: 369


« 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:
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:
BINMODE [#]file_number

TEXTMODE [#]file_number

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

Code:
BINMODE INPUT
BINMODE OUTPUT

TEXTMODE INPUT
TEXTMODE OUTPUT

The FREEFILE function returns an available file number.

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

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

Code:
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:
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:
record_position = POS(file_number)

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

Code:
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:
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:
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:
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:
PRINT CURDIR

CHDIR "directory_path"

ScriptBasic supports advisory locking of files.

Code:
LOCK #file_number,option

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

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

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

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

Returns true if the named file exists.

Code:
status = FILEEXISTS(file_name)


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")


File parameters can be set using the SET FILE directive.

Code:
SET FILE file_name parameter=value

Parameters:

Owner
CreateTime
ModifyTime
AccessTime


Get the time the file was modified last time.

Code:
fct = FILECREATETIME(file_name)


Get the time the file was accessed last time.

Code:
fat = FILEACCESSTIME(file_name)


Get the time the file was modified last time.

Code:
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:
FILECOPY file_name_from, file_name_to
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!