Create a text file with ASP using the File Scripting Object
<%
Dim oFilesys
Set oFilesys = Server.CreateObject("Scripting.FileSystemObject")
If NOT oFilesys.FileExists("C:\FSOfolder\newfile.txt") Then
oFilesys.CreateTextFile("C:\FSOfolder\newfile.txt")
Else
Response.Write "This file already exists."
End If
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.
If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|