Jump to content

If file exist not working as expected


narrator

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 comment
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 comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...