Things You'll Need:
- Windows machine running IIS / Classic ASP
-
Step 1
Create a FileSystemObject object. The FileSystemObject object is used to access files and folders, which is the basis for writing data to a file. The object can be created like so:
Set fso = CreateObject("Scripting.FileSystemObject") -
Step 2
Decide whether the data to be written will overwrite or add on (append) to existing data. When writing event data to a single log file for example, the data should be appended to whatever is currently in the log file. Otherwise, the log will only contain a record of the last event. Once the decision has been made, decide which file to write data into.
-
Step 3
Open the file. In classic ASP, a file must be opened before it can be written to. A file can be opened for overwriting like so:
Set logFile = fso.OpenTextFile("C:\MyFile.txt, 2)
The "2" means to overwrite any data already in the "C:\MyFile.txt" file. Opening a file for appending is accomplished using an "8" instead:
Set logFile = fso.OpenTextFile("C:\MyFile.txt, 8) -
Step 4
Write to the file. Once the file has been opened, it can then be written to:
logFile.WriteLine("Writing data to a file in Classic ASP is easy!") -
Step 5
Close the file. It is a good practice to always close a file after it has been opened, and then free up memory that was assigned to any objects created during the process:
logFile.Close
Set logFile = nothing
Set fso = nothing -
Step 6
Here is the data-writing code in its entirety:
Set fso = CreateObject("Scripting.FileSystemObject")
Set logFile = fso.OpenTextFile("C:\MyFile.txt, 8)
logFile.WriteLine("Writing data to a file in classic ASP is easy!")
logFile.Close
Set logFile = nothing
Set fso = nothing
Six lines of code is all it takes to write to a file in Classic ASP!







