1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Include some file, if not found than include other?

Discussion in 'C#' started by Error404, Jun 17, 2009.

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

    So I have this code on my page

    <!--#include file="somefile.txt"-->
    Code (markup):
    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

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

    Thanks...
     
    Error404, Jun 17, 2009 IP
  2. qwertyuiop[

    qwertyuiop[ Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I assume it's ASP Classic (VBScript) .. if so then do as follows:

    
    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 
    
    Code (markup):
     
    qwertyuiop[, Jun 17, 2009 IP
  3. qwertyuiop[

    qwertyuiop[ Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Btw, if it's an ASP.NET project then you can use the following code:

    
    If IO.File.Exists(Server.MapPath("somefile.txt")) Then
       ' do something 
    Else
       ' do something else
    End If
    
    Code (markup):
     
    qwertyuiop[, Jun 17, 2009 IP
  4. Corwin

    Corwin Well-Known Member

    Messages:
    2,438
    Likes Received:
    107
    Best Answers:
    0
    Trophy Points:
    195
    #4
    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:
    
    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
    Code (markup):
    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!
     
    Corwin, Jun 17, 2009 IP
  5. qwertyuiop[

    qwertyuiop[ Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 ...

    
    Dim content
    Set content = filesys.OpenTextFile("c:\somefile.asp")
    Response.Write(f.ReadAll)
    content.Close
    
    Code (markup):
    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
     
    qwertyuiop[, Jun 18, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    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.
     
    camjohnson95, Jun 20, 2009 IP
  7. Corwin

    Corwin Well-Known Member

    Messages:
    2,438
    Likes Received:
    107
    Best Answers:
    0
    Trophy Points:
    195
    #7
    Correct, it will output the file, but will not execute any ASP code inside.
     
    Corwin, Jun 20, 2009 IP
  8. DoDo Me

    DoDo Me Peon

    Messages:
    2,257
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    DoDo Me, Jun 20, 2009 IP
  9. DoDo Me

    DoDo Me Peon

    Messages:
    2,257
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #9
    DoDo Me, Jun 20, 2009 IP