Create a text file with ASP using the File Scripting Object

<%
Dim oFilesys
'this line creates an instance of the File Scripting Object named oFilesys
Set oFilesys = Server.CreateObject("Scripting.FileSystemObject")
'check if the file 'newfile.txt' does not exist
If NOT oFilesys.FileExists("C:\FSOfolder\newfile.txt") Then
'create file and place in its folder 'fsofolder' which we created on the C Drive
oFilesys.CreateTextFile("C:\FSOfolder\newfile.txt")
Else
'if the file exists let us know
Response.Write "This file already exists."
End If
'this line destroys the instance of the File Scripting Object named oFilesys
Set oFilesys = Nothing
%>

The code below writes text to the file we created.

<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim oFilesys, oFile
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFile = oFilesys.OpenTextFile("C:\FSOfolder\newfile.txt", ForWriting, True)
oFile.Write "Hello world!"
%>

The code below appends text to our existing file 'newfile.txt'.

<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim oFilesys, oFile
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFile = oFilesys.OpenTextFile("C:\FSOfolder\newfile.txt", ForAppending, True)
oFile.Write "This is my text file."
%>


Here's example code on how to open a file and write out it's contents.

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.