Sometimes a text file is preferred to a database. We can use text
files to maintain one line of information or many lines. This tutorial
will show you how use the File Scripting Object to both write to and
append to a text file. Nothing could be easier.
This is the code for APPENDING to a text file. This means what is
already in the file will NOT be overwritten.
<%
Dim Stuff, myFSO, WriteStuff 'this is what we will write
in the file
Stuff = "Here is some stuff to write in the file." 'this line creates an instance of the File Scripting Object named myFSO
Set myFSO = CreateObject("Scripting.FileSystemObject") 'this line opens the file,
notice the 8, it will cause the script to append to the file
Set WriteStuff = myFSO.OpenTextFile("C:\Inetpub\wwwroot\myNewFolder\myNewText.txt", 8, True) 'this line actually writes
STUFF from above to the file
WriteStuff.WriteLine(Stuff)
''this line closes the file
WriteStuff.Close 'this line destroys the instance of the File Scripting Object named
WriteStuff
SET WriteStuff = NOTHING 'this line destroys the instance of the File Scripting Object named myFSO
SET myFSO = NOTHING
%>
This is the code for WRITING to a file. It
will do just the opposite of the above. It will OVERWRITE anything
already in the file.
<%
Dim Stuff, myFSO, WriteStuff 'this is what we will write
in the file
Stuff = "Here is some stuff to write in the file." 'this line creates an instance of the File Scripting Object named myFSO
Set myFSO = CreateObject("Scripting.FileSystemObject") 'this line opens the file,
notice the 2, it will cause the script to write to the file
(overwriting existing text)
Set WriteStuff = myFSO.OpenTextFile("C:\Inetpub\wwwroot\myNewFolder\myNewText.txt",
2, True) 'this line actually writes
STUFF from above to the file
WriteStuff.WriteLine(Stuff)
''this line closes the file
WriteStuff.Close 'this line destroys the instance of the File Scripting Object named
WriteStuff
SET WriteStuff = NOTHING 'this line destroys the instance of the File Scripting Object named myFSO
SET myFSO = NOTHING
%>