Chapter 3 DynaScript Predefined Objects


file object

Object

Provides manipulation of files through the file system. These are files external to the Web site.

For information on manipulating documents internal to the Web site, see "site object".

Syntax

To use a file property:

file.propertyName

To use a file method:

file.MethodName( parameter ) 

Description

To manipulate a file, a file object is created to represent the file. To create a file object, use the file constructor. The syntax to create a new file object is:

fileObj = new File( fileName, accessMode);

The specified file is opened using the access mode provided, and a file object representing the open file is returned.

Example

This example reads information from FILE_A.DAT and adds it to the end of FILE_B.DAT, converting all tabs to spaces:

<!--SCRIPT 
  ifile = new File( "FILE_A.DAT", "r" );
  ofile = new File( "FILE_B.DAT", "a" );
  for( ch = ifile.ReadChar(); !ifile.eof; ch = ifile.ReadChar() ) {
    if( ch == "\t" ) {
        ofile.Write( "    " );
    } else {
        ofile.Write( ch );
    }
  }
  ifile.Close();
  ofile.Close();
-->

See also

"name property".

"mode property".

eof property

Syntax

file.eof 

Attributes

This property is read-only.

Description

Set to true when an input operation attempts to read past the end of the file.

Example

This example reads each character in the file input.txt:

<!--SCRIPT 
  inputFile = new File( "d:\\test\\input.txt", "r" );
  ch = inputFile.ReadChar ();
  while ( ! inputFile.eof ) {
    ch = inputFile.ReadChar();
  }
-->

errorNumber property

Syntax

file.errorNumber 

Attributes

This property is read-only.

Description

A number representing the error code of the last file method called. If an error occurred the errorNumber is non-zero. The values for errorNumber and errorString are system dependent and may differ between NT and versions of UNIX.

Example

This example displays the error number that results from opening a file in one mode and then opening it again in a different mode without closing the first instance:

<!--SCRIPT  
  inputFile = new File( "d:\\test\\input.txt", "r" );
  ch = inputFile.ReadChar ();
  while ( ! inputFile.eof ) {
     ch = inputFile.ReadChar();
     document.WriteLn( ch );
  } 
  inputFile = new File( "d:\\test\\input.txt", "w" );
  document.WriteLn( "The error number is: " + inputFile.errorNumber );
-->

This example checks for an error code of 0 and returns a message if a new file is created successfully:

<!--SCRIPT 

//Common Values for errorNumber on NT:
//0   No error
//1   No such file or directory
//6   Permission denied

    var fileName = "c:\\foo.txt";

    myFile = new File( fileName, "rt" );
    if( myFile.errorNumber != 0 ) {
        // if error was encountered:
        document.writeln( "Error number " + myFile.errorNumber + " was encountered: " + myFile.errorString );
    } else {
        document.writeln( myFile.name + " accessed successfully in " + myFile.mode + " mode." );
    }
-->

errorString property

Syntax

file.errorString 

Attributes

This property is read-only.

Description

A string containing an error message for the error code of the last file method called.

Example

This example displays an error message if there is a problem with the first file method called:

<!--SCRIPT 
  inputFile = new File( "d:\\test\\input5.txt","r" );
  document.WriteLn( inputFile.errorString );
  line = inputFile.ReadLine();
  document.WriteLn( inputFile.GetFilePtr() );
  inputFile.Close();
-->

If the file input5.txt cannot be found in the specified directory, the output looks like:

No such file or directory
null

name property

Syntax

file.name

Description

Name of the file to be manipulated (string).

Example

This example displays the name of the file represented by the file object myFile :

<!--SCRIPT
  myFile = new File( "d:\\test\\input.txt", "r" );
  document.WriteLn( myFile.name );
-->

The output from this example is:

input.txt

mode property

Description

The access mode with which the file was opened:

Access mode Description
r Open for read-only.
w Open for write, the file length to zero.
a Open for write at end of file.
rb Open binary file for reading.
rt Open text file for reading.
wb Create binary file for writing.
wt Create text file for writing.
ab Open binary file for write at end of file.
at Open text file for write at end of file.
r+ Open file for update (reading/writing).
w+ Create file for update (reading/writing).
a+ Open file for update at end of file.
rb+ Open binary file for update (reading/writing).
wb+ Create binary file for update.
ab+ Open binary file for update at end of file.
rt+ Open text file for update (reading/writing).
wt+ Create text file for update (reading/writing).
at+ Open text file for update at end of file.

Note  

Changing a file mode
Simply changing the file mode does not change the mode of the currently opened file. A call to Close followed by a call to Open is required to change a file mode.

Syntax

file.mode

Example

This example opens the file output.txt and appends the phrase "hello world" to the end of the file:

<!--SCRIPT
  outputFile = new File ( "d:\\test\\output.txt", "a" );
  outputFile.Write( "hello world" );
  outputFile.Close();
-->

Close method

Syntax

file.Close( ) 

Description

Closes the file associated with the file object.

Return

Boolean. Returns true or false indicating whether the file was successfully closed.

Example

This example opens and then closes the file input.txt:

<!--SCRIPT
  inputFile = new File ( "d:\\test\\input.txt", "r" );
  inputFile.Close()
-->

Delete method

Syntax

file.Delete( ) 

Description

Deletes the file associated with the file object. The file must be closed to be deleted.

Return

Boolean. Returns true or false indicating whether the file was successfully deleted.

Example

This example deletes the file input.txt:

<!--SCRIPT
  inputFile = new File ( "d:\\test\\input.txt", "r" );
  inputFile.Close();
  inputFile.Delete()
-->

GetFilePtr method

Syntax

file.GetFilePtr( )

Description

Returns the current file position. This position defines the position of the next character read or written.

Return

Integer. Position.

Example

This example displays the current position within the file input.txt:

<!--SCRIPT
  inputFile = new File( "d:\\test\\input.txt","r" );
  line = inputFile.ReadLine();
  document.WriteLn( inputFile.GetFilePtr() );
  inputFile.Close();
-->

Open method

Syntax

file.Open( )

Description

Opens the file specified by the name property in the read/write mode specified by the mode property.

Return

Boolean

Example

This example opens the file data.txt for reading, then reopens the file for writing:

<!--SCRIPT
  dataFile = new File ( "d:\\test\\data.txt", "r" );
  dataFile.Close( );
  dataFile.mode = "w";
  dataFile.Open( );
-->

Read method

Syntax

file.Read( numBytes )

Description

Reads the contents of a file. numBytes specifies the number of bytes to read. If no value is given, the entire contents of the file are read. This method is the recommended way of reading a binary file as ReadChar returns string data and ReadLine assumes line end characters are present.

Return

If the file is opened in text mode, the data returned is a string. If the file was opened in binary mode, the data returned is of type binary.

Example

This example reads the data from the input.txt file and displays it:

<!--SCRIPT
  inputFile = new File( "d:\\temp\\input.txt","r" );
  data=inputFile.Read();
  document.WriteLn( data );
 
-->

ReadChar method

Syntax

file.ReadChar( )

Description

The character at the current file position is returned, and the file position is advanced by one.

Return

String. Returns the character read.

Example

This example displays the first character of the file input.txt:

<!--SCRIPT
  inputFile = new File( "d:\\test\\input.txt","r" );
  document.WriteLn( inputFile.ReadChar() );
  inputFile.Close();
-->

ReadLine method

Syntax

file.ReadLine( )

Description

Reads and returns a string starting from the current file position to the next newline character or until end of file is reached. The newline character is not discarded.

Return

String. Returns the line read.

Example

This example displays the first line of the file input.txt:

<!--SCRIPT
  inputFile = new File( "d:\\test\\input.txt","r" );
  document.WriteLn( inputFile.ReadLine() );
  inputFile.Close();
-->

Seek method

Syntax

file.Seek(offset)

Description

Changes the current file position to the position indicated by offset. This position defines the position of the next character read or written. The position of the first character in the file is 0.

Return

Boolean. Indicates whether the current file position was successfully set.

Example

This example displays the fifth character from the file input.txt:

<!--SCRIPT
  inputFile = new File( "d:\\test\\input.txt","r");
  inputFile.Seek (4);
  document.WriteLn( inputFile.ReadChar() );
  inputFile.Close();
-->

Write method

Syntax

file.Write(s)

Description

The string value of s is written at the current file position. At the end of this operation, the current file position is set just after the written value.

Return

Boolean

Example

This example writes "hello world" to the file d:\test\output.txt, and erases all other text in the file:

<!--SCRIPT
  outputFile = new File ( "d:\\test\\output.txt","w" );
  outputFile.Write( "hello world" );
  outputFile.Close( );
-->

WriteLine method

Syntax

file.WriteLine(s)

Description

Identical to the Write method except that a new line is written following the written value.

Return

Boolean

Example

This example writes "hello world" to the file output.txt, and erases all other text in the file:

<!--SCRIPT
  outputFile = new File ( "d:\\test\\output.txt","w" );
  outputFile.WriteLine( "hello world" );
  outputFile.Close( );
-->

 


Copyright © 2001 Sybase, Inc. All rights reserved.