Reading and writing files in ASP

Explanation and reference on how you can read and write a file in ASP.

On some occasions it is necessary for our applications to read or write text files on the server.

For example, we could save all the report documents on our site in text files and from our ASP pages we could open those text files and display them within our site design. This is a common technique, which we also use in web development. Anyone who has programmed a bit will know the importance of handling text files in programs, so further examples of possible uses will not be necessary.

Since we are in ASP, we have to keep in mind that the files that we can manipulate are on the server, since ASP can access the server’s resources and not the client’s.

To read or write text files on the server using ASP, a File System Object (FSO) must be created, which is used to access the file system of the server where our pages are. For now we have not talked much about the FSO in , but we plan to do a series of reports to deal with it in depth. For now it will be enough that we know that to read or write a file we must necessarily rely on the FSO. To create a connection to the FSO in our ASP pages we do the following.

set con_FSO = createObject(“scripting.filesystemobject”)

At this point we already have access to the file system through the File System Object, our con_FSO variable stores the object that makes the connection to the file system. Now we must create the TextStream object, which will be the final object that we will need to have to read or write the file.

There are three methods in ASP to create the TexStream that are used in different cases, depending on the actions we intend to perform with the file. All three methods return a TextStream object.

See also  What alternatives to Express do we have in NodeJS?

CreateTextFile(file,overwrites,unicode)

This method will create a file in our file system and return a TextStream object, which is what we will use to move through the file, read or write. The file parameter is to indicate the file system path of the server where the file will be created. The other two parameters are optional. When set to true overwrites, it indicates that if there was a file with that name it is overwritten. When unicode is true, it indicates that the file should be created with the unicode character set.

OpenTextFile (file, access_type, create, format)

With this method we open a text file to read or write and it returns the TextStream object necessary to perform the actions on the file. The file parameter indicates the path of the file to create. The type_acceso parameter is optional, it indicates the type of access that we are going to perform, by default it is opened for reading, ForReading (1), we can also open it for writing ForWriting (2) and to add ForAppending (8). With the create parameter, also optional, it is indicated if the file should be created if it does not exist, by default it is not created. Finally, format is used to indicate the file format, ASCII is the default.

OpenAsTextStream (access_type, format)

It is the third method to obtain a TextStream, the difference is that it is applied to the File object that we have not seen, instead of the FileSystemObject object.

If we want to create a TextStream from an FSO we need to use one of the first two methods, the third being useful for creating a TexStream from a File object.

See also  jQuery function or $() function

The TextStream object

As we have pointed out, it is the one we use to move around the file and carry out actions on the text, focused on reading or writing. It has the following properties.

AtEndOfLine is true if we are at the end of the line in the file.

AtEndOfStream is true if we are at the end of the file.

Colum stores the value of the column of the current character in which we are located within the file.

Line stores the value of the current line.

The methods of the object are as follows.

Close() closes the file. Necessary once the work is finished.

Read(number) returns a number of characters from the file.

ReadAll() reads the entire file and returns it.

ReadLine() reads an entire line.

Skip(number) passes a given number of characters.

SkipLine() skips a line.

write(text) writes a given text into the file

WriteLine(text) writes a text and puts a line break at the end.

WriteBlankLines(number) places a given number of line breaks.

These notes will surely serve as a good reference to carry out any type of action on files, but before finishing we are going to see a complete example of working with files. We’re going to make a little script that creates a file and writes the numbers 0 through 9. Then we’ll close the file and reopen it for reading to output its content to the web page.

<%
‘create the file name

file= request.serverVariables(“APPL_PHYSICAL_PATH”) & “tests.txt”

‘we connect with the FSO

set confile = createObject(“scripting.filesystemobject”)

‘Create the TextStream object

set file = confile.CreateTextFile(file)

‘we write the numbers from 0 to 9

for i=0 to 9

file.write(i)

next

‘close the file

file.close()

‘reopen the file for reading

set file = confile.OpenTextFile(file)

‘read the content of the file

file_text = file.readAll()

See also  Change comma to period when using Mac numeric keypad

‘print the content of the file on the page

response.write(text_file)

‘close the file

file.close()

%>

In the first line of the code we create the name of the file. The filename is a string variable containing the absolute path of the file on the server system. As it probably happens with your ASP hosting, you don’t know in which directory and disk of the server your pages are hosted (unless you are the administrators or you are working locally). If so, you will need some mechanism to get the path where your files are and where you have read and write permissions. with the method
request.serverVariables(“APPL_PHYSICAL_PATH”) we get the physical path where our pages are. To this path we concatenate the name of the file that we want to create and we already have the full path of the file.

The rest of the lines can be well understood with the comments and notes above in this article.

It is important to note that to read and write to your directories you will need the corresponding read and write permissions. If you are doing tests on a Windows95 system you will not need permissions, since that system does not implement them. On slightly more serious servers like the ones your hosting provider will have, you will need to be granted permissions to the directories where you are going to write files. Read permissions are usually granted from the start.

That’s all for now, we hope this article is very useful to solve your doubts with the treatment of text files in ASP.

Loading Facebook Comments ...
Loading Disqus Comments ...