PDA

View Full Version : "File not found" error Substitution


biggs27
12-03-2002, 11:42 PM
I've got this chunk of code importing html into an asp:

<%
FileName = Server.MapPath(Request.Querystring("URL"))
Response.write CreateObject("Scripting.FileSystemObject").OpenTextFile _
(FileName, 1, False, False).readall
%>

If "URL" does not equal a valid URL, this error is displayed:

Microsoft VBScript runtime error '800a0035'

File not found

In this event, I would like to NOT display that error, but rather a different page in it's place (apologizing for the error, linking to my search page, etc.). What do I need to do to refer to another page (or display some alternate text)?

Thanks for your help!

Brian

whammy
12-04-2002, 01:23 AM
What I would do is check to see if the file exists, first...

Set fs = Server.CreateObject("Scripting.FileSystemObject")
If fs.FileExists(FileName) Then
'yay, it's there
Else
'do something else since it's not there!
End If


Check out http://www.w3schools.com/asp and look under FileSystemObject for a pretty in-depth reference. :)

biggs27
12-04-2002, 07:01 PM
:D Thanks whammy!!! Good call, and good reference... I was able to get it working and this is what it looks like.

<%
dim fs, file
Set fs=Server.CreateObject("Scripting.FileSystemObject")
file=(server.mappath(request.querystring("URL")))

IF fs.FileExists(file)=True THEN
FileName = Server.MapPath(Request.Querystring("URL"))
Response.write CreateObject("Scripting.FileSystemObject").OpenTextFile _
(FileName, 1, False, False).readall
ELSE%>
<!-- #Include VIRTUAL="/HR/filenotfound.htm" -->
<%End If%>

Thanks again,
Brian