Can't write file in classic asp

Bill Martin picture Bill Martin · May 22, 2009 · Viewed 8.3k times · Source

Ok, it's been a while since I've worked with classic asp so I'm a bit rusty. Here's my question.

I'm trying to write a file to the file system using FSO. The code below is very simple. However, the file is not appearing and no errors are appearing. I know it's running the code because I can add response.writes before and after this snippet and they both appear in the output. However, no file is created, no error is thrown. I've even changed it so it's a bogus path to force an error. No dice. I added everyone to have read and write on the directory permissions. Still the same.

Ideas?

Here's my code:

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("d:\test.txt", True)

'Display the contents of the text file
objTextStream.WriteLine "howdy"

'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing

Answer

Guffa picture Guffa · May 22, 2009

The only possible reason that the code would not produce an error message, is that you have this in your page:

On Error Resume Next

That's bad, for the reason that you have just seen. It just silently ignores any error messages, and leaves you without a clue to why it's not working as expected.

(It should only be used for isolated parts of the code where you anticipate an error, and actually check for an error condition after each operation.)

Remove that from your page, and the error message that you probably get is that the parameters are invalid for the call on this line:

Set objTextStream = objFSO.OpenTextFile("d:\test.txt", True)

You have forgotten the second parameter, which is the I/O mode. You should use the value 1 for writing:

Set objTextStream = objFSO.OpenTextFile("d:\test.txt", 1, True)

Alternatively, you can use the CreateTextFile method instead:

Set objTextStream = objFSO.CreateTextFile("d:\test.txt", True)