File System Object Help

Discussion in 'ASP General' started by boyd.roberts@heathwallace.com, Jun 4, 2008.

  1. Guest

    Hello All,

    I am trying to generate a list of files in a directory on a website,
    which only includes a defined list of file extentions.

    Below is my code but i have an issue that this only returns the first
    file in my includelist.


    <%
    Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Server.MapPath("."))
    Set objFolderContents = objFolder.Files

    for each objFileItem in objFolderContents

    includelist = "html, asp, php, htm"
    arrInclude = split(includelist, ",")

    pos = instr(objFileItem.Name, ".")
    extension = mid(objFileItem.Name, pos+1, len(objFileItem.Name)-pos)

    strinclude = false

    if extension = arrInclude(i) then
    strinclude = true
    end if

    if strinclude = true then
    %>
    <a href="<%=objFileitem.Name%>"><%=objFileitem.Name%></a><br>
    <%
    end if
    next
    %>

    Hope someone can show me the errors of my ways.

    Many Thanks
     
    , Jun 4, 2008
    #1
    1. Advertising

  2. wrote:
    > Hello All,
    >
    > I am trying to generate a list of files in a directory on a website,
    > which only includes a defined list of file extentions.
    >
    > Below is my code but i have an issue that this only returns the first
    > file in my includelist.
    >
    >
    > <%
    > Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
    > Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    > Set objFolder = objFSO.GetFolder(Server.MapPath("."))
    > Set objFolderContents = objFolder.Files
    >
    > for each objFileItem in objFolderContents
    >
    > includelist = "html, asp, php, htm"
    > arrInclude = split(includelist, ",")
    >
    > pos = instr(objFileItem.Name, ".")
    > extension = mid(objFileItem.Name, pos+1, len(objFileItem.Name)-pos)
    >
    > strinclude = false
    >
    > if extension = arrInclude(i) then


    You tell me: what is the value of i here?
    What do you think you would see if you did
    Response.Write i & "<BR>"

    > strinclude = true
    > end if
    >
    > if strinclude = true then
    > %>
    > <a href="<%=objFileitem.Name%>"><%=objFileitem.Name%></a><br>
    > <%
    > end if
    > next
    > %>
    >
    > Hope someone can show me the errors of my ways.
    >

    Try this:

    includelist = ",html,asp,php,htm,"
    if instr(includelist, "," & extension & ",") > 0 then
    strinclude = true
    else
    strinclude = false
    end if

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Jun 4, 2008
    #2
    1. Advertising

  3. Old Pedant Guest

    > includelist = ",html,asp,php,htm,"
    > if instr(includelist, "," & extension & ",") > 0 then
    > strinclude = true
    > else
    > strinclude = false
    > end if


    Well, since my screen name is indeed old PEDANT...

    Why not simply

    <%
    ....
    strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0
    ....
    %>

    Don't need to use an IF test to set a boolean value based on something that
    is already a boolean value.
     
    Old Pedant, Jun 5, 2008
    #3
  4. <> wrote in message
    news:...
    > Hello All,
    >
    > I am trying to generate a list of files in a directory on a website,
    > which only includes a defined list of file extentions.
    >
    > Below is my code but i have an issue that this only returns the first
    > file in my includelist.
    >
    >
    > <%
    > Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
    > Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    > Set objFolder = objFSO.GetFolder(Server.MapPath("."))
    > Set objFolderContents = objFolder.Files
    >
    > for each objFileItem in objFolderContents
    >
    > includelist = "html, asp, php, htm"
    > arrInclude = split(includelist, ",")
    >
    > pos = instr(objFileItem.Name, ".")
    > extension = mid(objFileItem.Name, pos+1, len(objFileItem.Name)-pos)
    >
    > strinclude = false
    >
    > if extension = arrInclude(i) then
    > strinclude = true
    > end if
    >
    > if strinclude = true then
    > %>
    > <a href="<%=objFileitem.Name%>"><%=objFileitem.Name%></a><br>
    > <%
    > end if
    > next
    > %>
    >
    > Hope someone can show me the errors of my ways.
    >


    In addition to the help the others have given you should bear in mind that
    the following is a legal file name:-

    This.is.a.file.htm

    Use-

    sIncludeList = ",html,asp,php,htm,"

    ....

    sExtension = objFSO.GetExtenstionName(objFileItem.Name)

    bInclude = Instr(sIncludeList, "," & sExtension & ",") > 0


    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Jun 5, 2008
    #4
  5. Old Pedant wrote:
    >> includelist = ",html,asp,php,htm,"
    >> if instr(includelist, "," & extension & ",") > 0 then
    >> strinclude = true
    >> else
    >> strinclude = false
    >> end if

    >
    > Well, since my screen name is indeed old PEDANT...
    >
    > Why not simply
    >
    > <%
    > ...
    > strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0
    > ...
    > %>
    >
    > Don't need to use an IF test to set a boolean value based on
    > something that is already a boolean value.


    That's how I would do it, but my goal was to make sure the OP understood
    what was going on, not minimizing the number of lines of code. :)

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Jun 5, 2008
    #5
  6. Old Pedant wrote:
    >> includelist = ",html,asp,php,htm,"
    >> if instr(includelist, "," & extension & ",") > 0 then
    >> strinclude = true
    >> else
    >> strinclude = false
    >> end if

    >
    > Well, since my screen name is indeed old PEDANT...
    >
    > Why not simply
    >
    > <%
    > ...
    > strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0
    > ...
    > %>
    >
    > Don't need to use an IF test to set a boolean value based on
    > something that is already a boolean value.


    :)
    Actually, since we are being pedantic, InStr() does not return a boolean
    value: it returns a number signifying the position of the search string
    in the to-be-searched string. Your shortcut is relying on the
    coincidence that zero corresponds to the value of the boolean false in
    vbscript, as well as relying on an implicit conversion of an integer to
    a boolean.

    It's fortunate that vbscript is no longer being developed so there is no
    chance that Instr() will ever be revised to return a zero-based index
    for the character position ...

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Jun 5, 2008
    #6
  7. Old Pedant Guest

    > Actually, since we are being pedantic, InStr() does not return a boolean
    > value: it returns a number signifying the position of the search string
    > in the to-be-searched string. Your shortcut is relying on the
    > coincidence that zero corresponds to the value of the boolean false in
    > vbscript, as well as relying on an implicit conversion of an integer to
    > a boolean.


    Ummm...not, it's not. Read it again:

    strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0

    Look at the "> 0" there at the end.

    That's why I noted "Don't need to use an IF test to set a boolean value
    based on something that is already a boolean value." That is, the "> 0"
    comparison is already producing the boolean value.

    I suppose it would have been clearer to have written it as

    strInclude = ( InStr( ",html,asp,php,htm," , "," & extension & "," ) >
    0 )

    But since extra parentheses in VBScript cause an extra dummy Variant to be
    pushed onto the operator stack, I avoided that. Yeah, I know. Optimizing
    the wrong thing.

    In reality, of course, there's no reason in his code to even *HAVE* that
    strInclude variable. Easier, faster, simpler to just do the IF test and
    driectly affect the HTML:

    <%
    ....
    for each objFileItem in objFolderContents
    fName = objFileItem.Name
    extension = Mid(fName, InStrRev(fName,".")+1)
    If InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0 Then
    %>
    <a href="<%=fName%>"><%=fName%></a><br>
    <%
    end if
    next
    %>
    ....

    But never mind. Getting far off track now.
     
    Old Pedant, Jun 5, 2008
    #7
  8. Old Pedant wrote:
    >> Actually, since we are being pedantic, InStr() does not return a
    >> boolean value: it returns a number signifying the position of the
    >> search string in the to-be-searched string. Your shortcut is relying
    >> on the coincidence that zero corresponds to the value of the boolean
    >> false in vbscript, as well as relying on an implicit conversion of
    >> an integer to a boolean.

    >
    > Ummm...not, it's not. Read it again:
    >
    > strInclude = InStr( ",html,asp,php,htm," , "," & extension & ","
    > ) > 0
    >
    > Look at the "> 0" there at the end.

    Oops, I did miss that, A lot of people think leaving that ">0" bit off is
    very smart.

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Jun 6, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathy Burke
    Replies:
    2
    Views:
    5,172
    Kevin Spencer
    Aug 1, 2003
  2. Showjumper
    Replies:
    2
    Views:
    1,835
    Showjumper
    Aug 2, 2003
  3. Imran Aziz
    Replies:
    4
    Views:
    8,765
    ljenner01
    Jan 19, 2011
  4. keithb
    Replies:
    3
    Views:
    18,782
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 7, 2006
  5. Replies:
    0
    Views:
    902
Loading...

Share This Page