Digital Point Forums
Money Order

Go Back   Digital Point Forums > Design & Development > Programming > ASP
Google Analytics
Log In to view
your analytics

Reply
 
Thread Tools
  #1  
Old Jun 17th 2009, 10:40 am
Error404 Error404 is offline
Peon
 
Join Date: Jan 2007
Posts: 16
Error404 is on a distinguished road
Include some file, if not found than include other?

Hi, I'm very new to ASP...

So I have this code on my page

Code:
<!--#include file="somefile.txt"-->
which will show textual content of somefile.txt

Now what I want is that if somefile.txt is not found on server, I wan't the content of another textual file to be shown (someotherfile.txt)

so I need something like

Code:
<!--#include file="somefile.txt"-->
if file not found then
<!--#include file="someotherfile.txt"-->
Of course this is not correct code, so that is where I need your help... How do I write this code in ASP?

Thanks...
Reply With Quote
  #2  
Old Jun 17th 2009, 2:10 pm
qwertyuiop[ qwertyuiop[ is offline
Grunt
 
Join Date: May 2007
Posts: 33
qwertyuiop[ is on a distinguished road
I assume it's ASP Classic (VBScript) .. if so then do as follows:

Code:
Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
filesys.CreateTextFile "c:\somefile.txt", True
If filesys.FileExists("c:\somefile.txt") Then
   <!--#include file="somefile.txt"-->
Else
   <!--#include file="someotherfile.txt"-->
End If
Reply With Quote
  #3  
Old Jun 17th 2009, 2:13 pm
qwertyuiop[ qwertyuiop[ is offline
Grunt
 
Join Date: May 2007
Posts: 33
qwertyuiop[ is on a distinguished road
Btw, if it's an ASP.NET project then you can use the following code:

Code:
If IO.File.Exists(Server.MapPath("somefile.txt")) Then
   ' do something 
Else
   ' do something else
End If
Reply With Quote
  #4  
Old Jun 17th 2009, 5:35 pm
Corwin's Avatar
Corwin Corwin is offline
Twilight Vanquisher
 
Join Date: Feb 2006
Location: Boston / New York
Posts: 683
Corwin will become famous soon enough
Thumbs up

Quote:
Originally Posted by qwertyuiop[ View Post
I assume it's ASP Classic (VBScript) .. if so then do as follows:

Code:
Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
filesys.CreateTextFile "c:\somefile.txt", True
If filesys.FileExists("c:\somefile.txt") Then
   <!--#include file="somefile.txt"-->
Else
   <!--#include file="someotherfile.txt"-->
End If
No, that's a common mistake. If the file doesn't exist, the above code is going to throw a compile error and stop the page . An #include is added to the file BEFORE any code is compiled.



However, THIS will work:
Code:
Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
filesys.CreateTextFile "c:\somefile.asp", True
IF filesys.FileExists("c:\somefile.asp") THEN
   Server.Execute("somefile.asp")
ELSE
   Server.Execute("someotherfile.asp")
END IF
Understand that while any file you run in Server.Execute will have access to all your variables and session, it will NOT have access to defined subroutines and functions.

Let me know if this helps you out!
__________________
New York Stairlifts | Trade Currency | Don't Worry, Be HAPPY!
Reply With Quote
  #5  
Old Jun 18th 2009, 4:24 pm
qwertyuiop[ qwertyuiop[ is offline
Grunt
 
Join Date: May 2007
Posts: 33
qwertyuiop[ is on a distinguished road
I agree Include function as is will not work ... however you can use the fylesys object to read the somefile.txt content and show up its content ...

Code:
Dim content
Set content = filesys.OpenTextFile("c:\somefile.asp")
Response.Write(f.ReadAll)
content.Close
However please notice that i am not ASP Classic developer .. i am coding asp.net on daily basis but VBScript is pretty similar to VB.NET anyway, just a little bit trickier. lol
Reply With Quote
  #6  
Old Jun 20th 2009, 9:33 am
camjohnson95 camjohnson95 is offline
Hand of A'dal
 
Join Date: Nov 2008
Posts: 386
camjohnson95 will become famous soon enough
Quote:
Originally Posted by qwertyuiop[ View Post
I agree Include function as is will not work ... however you can use the fylesys object to read the somefile.txt content and show up its content ...

Code:
Dim content
Set content = filesys.OpenTextFile("c:\somefile.asp")
Response.Write(f.ReadAll)
content.Close
However please notice that i am not ASP Classic developer .. i am coding asp.net on daily basis but VBScript is pretty similar to VB.NET anyway, just a little bit trickier. lol
I'm not completely sure but won't using the above code output the code within 'somefile.asp', rather than execute it? Otherwise, if the file is only text and/or HTML then it should work fine.
Reply With Quote
  #7  
Old Jun 20th 2009, 11:35 am
Corwin's Avatar
Corwin Corwin is offline
Twilight Vanquisher
 
Join Date: Feb 2006
Location: Boston / New York
Posts: 683
Corwin will become famous soon enough
Quote:
Originally Posted by camjohnson95 View Post
I'm not completely sure but won't using the above code output the code within 'somefile.asp', rather than execute it? Otherwise, if the file is only text and/or HTML then it should work fine.
Correct, it will output the file, but will not execute any ASP code inside.
__________________
New York Stairlifts | Trade Currency | Don't Worry, Be HAPPY!
Reply With Quote
  #8  
Old Jun 20th 2009, 6:09 pm
DoDo Me DoDo Me is offline
Twilight Vanquisher
 
Join Date: Apr 2008
Posts: 502
DoDo Me will become famous soon enough
You have to read text file and execute it with execute(str) in ASP
This is the only way to dynamic include a file in ASP
__________________
Referer.us
Short a URL and Hide Referer, on
Reply With Quote
  #9  
Old Jun 20th 2009, 6:10 pm
DoDo Me DoDo Me is offline
Twilight Vanquisher
 
Join Date: Apr 2008
Posts: 502
DoDo Me will become famous soon enough
http://msdn.microsoft.com/en-us/library/ms525849.aspx

Server.Execute Method
__________________
Referer.us
Short a URL and Hide Referer, on
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Include file with variable, like file.php?var=1? 123GoToAndPlay PHP 4 Apr 16th 2008 12:29 pm
How to add php include & cgi include statments to html file spencermjax HTML & Website Design 0 Apr 16th 2008 6:50 am
How to include a out file? yaman PHP 6 Mar 30th 2008 12:17 am
Include a php Template file in a HTML file goldendragon PHP 7 Aug 31st 2007 10:31 am
include('$file'); promotingspace.net PHP 12 Jun 28th 2007 11:38 am


All times are GMT -8. The time now is 12:32 pm.