ASP FileExists returns true when it should return false

When I use the ASP FileSystemObject.FileExist function to check for a file in a subfolder, the return value is TRUE even though the file I'm checking for is NOT present.

Strangely this seems to occur in only once place:- other parts of the code where the same function is used work correctly.

By way of further explanation, the script containing the problematic code is called via an SSI; a technique used extensively throughout the website.

I've already spent a whole frustrating day on this seemingly simple problem so I'm inclined to offer a substantial number of points for the solution.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I know this may seem obvious, but, check to make sure the file is not hidden.
opaconsAuthor Commented:
It's not hidden.

I've even deleted an existing file to see if the behaviour persists and it does.
Can you post the code please, since it only happens in that one part of the code and not everywhere there may be something else affecting the result.
opaconsAuthor Commented:
Line 5 should return false because the file does not exist in the named folder. Instead, a "include file not found" error is displayed because of an attempt to execute line 6 rather than line 8.
1 <%@ Language="VBScript" %>
2 <% Option Explicit %>
3 <!-- #include virtual="/shop/inc/incfiles.asp" -->
1 <%
2 Dim fso
3 Set fso = Server.CreateObject("Scripting.FileSystemObject")
4 %>
5 <% if fso.FileExists(Server.MapPath("/newsite/inc/modAdovbsG.asp")) then %>
6 <!--#include virtual="/newsite/inc/modAdovbsG.asp" -->
7 <% else %>
8 <!--#include virtual="/inc/modAdovbsG.asp" -->
9 <% end if %>

Open in new window

You are receiving the "include file not found" error because the Server Side Includes (SSI) lines that start with <!--#include... are both ALWAYS processed. This is a preprocessing step before the VBScript is executed.

When you request the ASP page from the web server, the web server processes the request as follows:

1) Takes the code from the "incfiles.asp" file and attempts to merge in the code from the "/newsite/inc/modAdovbsG.asp" file (in place of the SSI statement).
2) Next it attempts to merge in the code from the "/inc/modAdovbsG.asp" file (in place of that SSI statement).
3) The server would then begin executing the VBScript resulting from the merge of the 3 files. In your case, it never reaches this step because it fails when trying to preprocess the non-existent include file.

This is actually somewhat tricky to do, but the code snippet gives you a possible workaround solution. It may or may not work depending on what code you have in your included file.

' Include this function on your page to provide dynamic file inclusion.
Public Sub IncludeFile(filename)
   Dim fso, file, sourceCode
   Set fso = Server.CreateObject("Scripting.FileSystemObject")
   Set file = fso.OpenTextFile(Server.MapPath(filename))
   sourceCode = file.ReadAll()
   sourceCode = Replace(sourceCode, "<" & "%", "")
   sourceCode = Replace(sourceCode, "%" & ">", "")
End Sub
... Do your page setup as in your code example ...
If fso.FileExists(Server.MapPath("/newsite/inc/modAdovbsG.asp")) Then
End If

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.