Jump to content

If file exist not working as expected


Recommended Posts

Hi,On my site, categories are organized into folders. When there's no body to the page, I use a standard body ( ../inc/body.inc). If there is a body.inc in the same folder, I want it to use that instead. I created the following code for that purpose, but regardless whether there is a body.inc file within the same folder or not, it only uses my standard body file (../inc/body.inc). What's wrong with the code? I've tried changing "body.inc" in lines 3 and 4 to "./body.inc" and ".\body.inc", neither of which makes any difference. I got the basis for the code from here.<%Set fs=Server.CreateObject("Scripting.FileSystemObject")If (fs.FileExists("body.inc"))=true ThenSet f=fs.OpenTextFile(Server.MapPath("body.inc"), 1)Response.Write(f.ReadAll)f.CloseElseSet f=fs.OpenTextFile(Server.MapPath("../inc/body.inc"), 1)Response.Write(f.ReadAll)f.CloseEnd IfSet f=Nothingset fs=nothing%>Thanks,Cheers

Link to post
Share on other sites
Use Server.MapPath when you're checking the filename, not just when you open it.
Woohoo!! Thank You.. it works now. :)<%Set fs=Server.CreateObject("Scripting.FileSystemObject")If (fs.FileExists(Server.MapPath("body.inc")))=true ThenSet f=fs.OpenTextFile(Server.MapPath("body.inc"), 1)Response.Write(f.ReadAll)f.CloseElseSet f=fs.OpenTextFile(Server.MapPath("../inc/body.inc"), 1)Response.Write(f.ReadAll)f.CloseEnd IfSet f=Nothingset fs=nothing%>
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...