Tuesday, October 17, 2006

Write to a File in ASP

Here's a simple script that will create (or open) a file and write to it:
<% const ForAppending=8 const TristateFalse=0  dim oFile, oFSO, strFileName  'Create a filesystem object    Set oFSO = CreateObject("Scripting.FileSystemObject")  'The path and strFileName to write to strFileName="c:\inetpub\wwwroot\MyWebSite\files\testfile.txt"  'Open the file or create a new if it does not already exist Set oFile = oFSO.OpenTextFile(strFileName, ForAppending, true, TristateFalse)  'Write to the file oFile.Writeline "This is a test!"  'Close the file oFile.Close  'Free the objects set oFile=nothing set oFSO=nothing %>
If the file doesn't exist, it will be created and the line written to it. If the file already exists, it will be opened and the new line added at the bottom of all the existing lines.

If you are executing this script from a web page, then you need to make sure the the IUser account has been granted write permissions to whatever directory the file is in. In the example, you would need to make sure IUser has write permissions to the 'files' folder.

No comments: