views:

799

answers:

2

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
+1  A: 

Try this:

<%

if Append = true then
   iMode = 8
else 
   iMode = 2
end if
set oFs = server.createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile("C:\wwwroot\Test.txt", iMode, True)
oTextFile.Write "Test Content"
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

%>

I tried this locally, after setting the permissions on my directory and it worked.

You can get the original source from here: http://www.freevbcode.com/ShowCode.Asp?ID=89

Jason Heine
Only thing I would do differently is to check to see if the file exists first. Unless iMode of 2 is to create the file...been a while and dont remember all the modes :)
David Yancey
Code looks great but I got the same result. No file, no error. Anything else to try or check?
asp316
Try creating a directory on your drive and set the permissions to everyone with write access and see if that works. If it does then it might be a permission issue. The only thing with that, is that if it is a permission issue, your code should report that you don't have access. Hmm...Thinking
Jason Heine
Created a directory and granted everyone full access. Same result. Weird.
asp316
You don't have an on error resume next in there right?
Jason Heine
Additional note: are you trying this with a blank asp page? no includes?
Jason Heine
Nope. Using the existing page with all of it's coded glory. WAAAAY up in the code was a hidden 'On Error Resume Next'. ARRRRRGGG. <comment, click> Was able to debug and now it works fine now!Thanks for the help!!!
asp316
Excellent! Glad I could help.
Jason Heine
+2  A: 

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)
Guffa
I wish I could check 2 correct answers. We just found that way up in the code was an evil "On Error Resume Next". Removed that . Works great. Thanks for the help!!!
asp316