SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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
%>
Further readings
Server.MapPath to convert virtual path to physical path of files and directory.
FileSystemObject:Checking file or folder exist by using File system object .
Getting the file name from full path of the current running script in ASP.
Getting the file extension and file name by using FSO
Deleting file by using DeleteFile in FSO
Getting the Last updating and creation date and time of a file.
Listing all files present inside a folder using FileSystemObject
Reading data from a text file using File System Object
Writing data to a text file using File System Object
Read & Writing data to a text file: Developing a page hit counter
Appending data to a text file: Storing emails in a text file
 
Scripts
PHP
JavaScript
All ASP Tutorials
File Management
MapPath
FileSystemObject
append
name
Updating Time
Listing all files
Reading file
Writing file
Read & Write
File type
Delete File
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.