Jump to content

If file exist not working as expected


narrator
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...