• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 763
  • Last Modified:

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.
  • 2
  • 2
1 Solution
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


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now