|
In an active server page, one can create, open and read or write to text files. This is done by the following processes:
- creating a FileSystemObject. This is the same syntax as a recordset or a connection. Specifically:
Set user_name_of_file_object = CreateObject("Scripting.FileSystemObject")
This assigns a name to the FileSystemObject. For example, using fs as the name of the FileObject:
Set fs=CreateObject("Scripting.FileSystemObject")
- next we can create files with the following sytntax:
Set user_name_of_file = user_name_of_file_object.CreateTextFile("real path and filename", over-write_switch)
Here:
- user_name_of_file is the internal VBscript handle to be used with file.
- user_name_of_the_file_object is the name used in the CreateObject step above.
- real path and filename is the fully specified filename
- if over-write_switch is set to true, then the file can be overwritten, if set to false, the the file cannot be overwritten. Default is false.
For example:
Set fred_file = fs.CreateTextFile("c:\Students\glxxx\some.txt",true)
Here:
- fred_file is the Vbscript name for the file
- fs is the FileSystemObject from the previous step
- c:\Students\glxxx\some.txt is the real path and filename
- true allows the file to be overwritten
- next we can open a file with the following sytntax:
Set user_name_of_file = user_name_of_file_object.OpenTextFile("real path and filename", i-mode, create)
Here:
- user_name_of_file is the internal VBscript handle to be used with file.
- user_name_of_the_file_object is the name used in the CreateObject step above.
- real path and filename is the fully specified filename
- i-mode is set to either ForReading, ForWriting, or ForAppending. ForReading opens the files for input, ForWriting opens the file for output and ForAppending opens the file for output -- but note that if the file exists, all new data written to the file is appended to the existing file contents.
Your will need to add the following definitions to your asp program:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
in order to define the i-mode constants. If you don't include the definitions you must use the numberic values (i.e., 1,2 or 8) in the OpenTextFile arguments.
- create is either true (create the file) or false (the file already exists). Default is false.
For example:
Set fred_file = fs.OpenTextFile("c:\Students\glxxx\some.txt",ForReading, false)
Here:
- fred_file is the Vbscript name for the file
- fs is the FileSystemObject from the previous step
- c:\Students\glxxx\some.txt is the real path and filename
- ForReading makes the file read-only
- false indicates that the file is NOT to be created (it already exists)
NOTE Both the object.OpenTextFile and the object.CreateTextFile allow access to the file immediately. Both are NOT required (i.e., both methods prepare the file for processing -- you do not have to use both create and open -- both methods can without the other)
You can also Move and Copy files using FSOs:
The MoveFile method in effect renames a file. The usage is:
user_name_of_the_file_system_object.MoveFile old_file_name , new_file_name
The CopyFile method makes another copy of a file. The usage is:
user_name_of_the_file_system_object.CopyFile source_file_name , new_file_name
TextStream files have the following methods:
- object.read(number of characters)
Reads number of characters from the file. For example:
alice = fred_file.read(20)
reads 20 characters from fred_file and stores the resulting string in alice.
- object.readline
readline returns a string which is the next line from the file. It does not store the cr/lf characters in the string (i.e., the end of line character and carriage return character). For example:
alice = fred_file.readline
reads the next line (record) from fred_file and stores the resulting string in alice.
- object.skip(number_of_characters)
skips number_of_characters. For example:
fred_file.skip(15)
skips the next 15 characters.
- object.skipline
Skips the next line read from the file. For example,
fred_file.skipline
skips the next line from fred_file
- object.write(string)
writes the string to the file named fred_file. For example:
fred_file.write ("this is some text")
write the string "this is some text" to the file named fred_file.
- object.writeline(string)
writes the string to the file named fred_file AND inserts a lf (linefeed) character. This differs from the write method that only writes the string but does not provide a terminating character for the line (record).
fred_file.writeline ("this is some text")
write the string "this is some text" and a lf character to the file named fred_file.
- object.writeblanklines(number)
writes number blank lines to the file. This actually only generates number of lf (line feed) characters. For example:
fred_file.writeblanklines (5)
writes 5 blank lines to the file.
- object.close
closes the file. For example:
fred_file.close
closes the file named fred_file.
TextStream files have four properties:
- object.AtEndofStream
This Boolean variable is either true or false. It applies only to TextStream files that are opened ForReading. If the file is at the end, AtEndOfStream is false otherwise it is true. For example, here is a typical read loop:
Do While fred_file.AtEndOfStream <> true
alice=Fred_file.readline
.
. work goes here
.
Loop
- object.AtEndofLine
This Boolean variable is either true or false. It applies only to TextStream files that are opened ForReading. If the current line is not at the end, AtEndOfLine is false otherwise it is true. For example, here is a typical read loop to process characters from a line in a file one at a time:
Do While fred_file.AtEndOfLine <> true
alice=Fred_file.read(1)
.
. gets characters one at a time from this line
.
Loop
- object.column
This returns the column position on the current line.
- object.line
This returns the current line number within the file.
|