Writing data to text file using FSO

As you have seen how to create FSO (file System object) and use it to read the content of a text file. Now we will try to use this FSO and write data to a text file. Before reading this please read the details of file system object and how it works.

For writing to a file we have to open the file in writing mode. This line within our script ( full code is given at the end of this tutorial ) does that.

Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Writing)

In the above line we have used the constant Writing and its value is declared at the starting as 2. You can write 2 in the place for opening the file in writing mode.
Please note that to open file in writing mode, we should have write permission for the file , otherwise we will get an error message similar to this.

Error Type:
Microsoft VBScript runtime (0x800A0046)
Permission denied
writing.asp, line 28

In the writing mode fresh data is written to the file, so old data is erased every time the file is opened in write mode.

Writing Data into the file
We can use different types to write data to the file. One is file simple write like this

OpenFileobj.Write("my data here")

This will write the string my data here to the file. Next we will write one line of data to the file.

OpenFileobj.WriteLine(" Welcome to Plus2net.com ")

The above line will add one line of text Welcome to Plus2net.com to the file. This text will be added to the previous text string ( my data here ) as a continuous within one line. But know one line break will be added at the end of the second string as we have used WriteLine command. This command will always add one line break after adding the string ( not before ). To see the line breaks open your text file in notepad. Your web browser won't show line breaks as browser understand <br> tag as line breaks.

Now let us learn how to add blank lines. Here is the command.

OpenFileobj.WriteBlankLines(4)

Here we are adding four line breaks. Using this combination we can create the script to write data to a file. Here is the full script.

<%
Const Writing=2
Dim OpenFileobj, FSOobj,FilePath
FilePath=Server.MapPath("text.txt") ' located in the same directory
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Writing)
OpenFileobj.Write("my data here")
OpenFileobj.WriteLine(" Welcome to Plus2net.com ")
OpenFileobj.Write("Learn ASP- VBScript the programming language ")
OpenFileobj.WriteBlankLines(4)
OpenFileobj.WriteLine("You can develop interactive web pages ")
OpenFileobj.Write("Thank you & Visit again")
OpenFileobj.Close
Set OpenFileobj = Nothing
Else
Response.Write "File does not exist"
End if
Set FSOobj = Nothing
%>

Be the first to post comment on this article :





Post Comment This is for short comments only. Use the forum for more discussions.




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer