How to address one item in a collection object

Discussion in 'ASP General' started by Guojun Zhu, Jun 30, 2005.

  1. Guojun Zhu

    Guojun Zhu Guest

    I am using VBscript in ASP. I am trying to address one file in a folder
    object. Below is the relevant code


    Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
    ' MyFolders is contents of folder containing photos.asp
    Set MyFolders=MyDirectory.GetFolder(Server.MapPath("./"))

    Then I try to access the first file in the "MyFolder"
    I tried
    1. Set filefound=MyFiles.files.item(1)
    2. Set filefound=MyFiles.files(1)
    3. Set filefound=MyFiles.files[1]
    Neither work. There is compilation error.
    But

    For each filefound in MyFiles.files

    works
    "myfiles.files" is a collection object of file objects, isn't it?

    Any experts help me please. Thanks.
     
    Guojun Zhu, Jun 30, 2005
    #1
    1. Advertising

  2. Guojun Zhu

    Curt_C [MVP] Guest

    Guojun Zhu wrote:
    > I am using VBscript in ASP. I am trying to address one file in a folder
    > object. Below is the relevant code
    >
    >
    > Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
    > ' MyFolders is contents of folder containing photos.asp
    > Set MyFolders=MyDirectory.GetFolder(Server.MapPath("./"))
    >
    > Then I try to access the first file in the "MyFolder"
    > I tried
    > 1. Set filefound=MyFiles.files.item(1)
    > 2. Set filefound=MyFiles.files(1)
    > 3. Set filefound=MyFiles.files[1]
    > Neither work. There is compilation error.
    > But
    >
    > For each filefound in MyFiles.files
    >
    > works
    > "myfiles.files" is a collection object of file objects, isn't it?
    >
    > Any experts help me please. Thanks.
    >
    >
    >
    >
    >


    I believe you will have to loop it.


    --
    Curt Christianson
    site: http://www.darkfalz.com
    blog: http://blog.darkfalz.com
     
    Curt_C [MVP], Jun 30, 2005
    #2
    1. Advertising

  3. Guojun Zhu wrote:
    > I am using VBscript in ASP. I am trying to address one file in a
    > folder object. Below is the relevant code
    >
    >
    > Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
    > ' MyFolders is contents of folder containing photos.asp
    > Set MyFolders=MyDirectory.GetFolder(Server.MapPath("./"))
    >
    > Then I try to access the first file in the "MyFolder"
    > I tried
    > 1. Set filefound=MyFiles.files.item(1)
    > 2. Set filefound=MyFiles.files(1)
    > 3. Set filefound=MyFiles.files[1]
    > Neither work. There is compilation error.
    > But
    >
    > For each filefound in MyFiles.files
    >
    > works
    > "myfiles.files" is a collection object of file objects, isn't it?
    >

    Yes. Unfortunately it is not a "standard" (COM) collection - it's more like
    a dictionary object, i.e., it is keyed, not indexed. You can only access an
    individual element by using its name. So, believe it or not, to reference an
    item by number, you have to use a loop (you might want to encapsulate it in
    a function):

    function getFileByIndex(pFiles, idxDesired)
    dim idx,filefound
    if idxDesired >= pFiles.Count then idxDesired=pFiles.Count -1
    idx=0
    For each filefound in pFiles.files
    if idxDesired = idx then
    exit for
    else
    idx=idx+1
    end if
    next
    getFileByIndex=filefound
    end function

    HTH,
    Bob Barrows
    --
    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 30, 2005
    #3
  4. Bob Barrows [MVP] wrote:
    > getFileByIndex=filefound


    Oops, this should be:

    Set getFileByIndex=filefound

    Bob
    --
    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 30, 2005
    #4
  5. Guojun Zhu

    Guojun Zhu Guest

    Thanks. I have to say this is a little stupid though.
    "Guojun Zhu" <> wrote in message
    news:#...
    > I am using VBscript in ASP. I am trying to address one file in a folder
    > object. Below is the relevant code
    >
    >
    > Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
    > ' MyFolders is contents of folder containing photos.asp
    > Set MyFolders=MyDirectory.GetFolder(Server.MapPath("./"))
    >
    > Then I try to access the first file in the "MyFolder"
    > I tried
    > 1. Set filefound=MyFiles.files.item(1)
    > 2. Set filefound=MyFiles.files(1)
    > 3. Set filefound=MyFiles.files[1]
    > Neither work. There is compilation error.
    > But
    >
    > For each filefound in MyFiles.files
    >
    > works
    > "myfiles.files" is a collection object of file objects, isn't it?
    >
    > Any experts help me please. Thanks.
    >
    >
    >
    >
    >
     
    Guojun Zhu, Jun 30, 2005
    #5
  6. Guojun Zhu

    Curt_C [MVP] Guest

    Guojun Zhu wrote:
    > Thanks. I have to say this is a little stupid though.


    Its what happens when you dont get a nice OO system to work with...
    Part of the many difficulties with VBScript and such.


    --
    Curt Christianson
    site: http://www.darkfalz.com
    blog: http://blog.darkfalz.com
     
    Curt_C [MVP], Jun 30, 2005
    #6
    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. Guojun Zhu
    Replies:
    1
    Views:
    504
  2. Dylan
    Replies:
    5
    Views:
    444
    Daniel T.
    Mar 22, 2005
  3. Pradeep
    Replies:
    2
    Views:
    695
    Patricia Shanahan
    Jan 24, 2007
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,027
    Øyvind Isaksen
    May 18, 2007
  5. Daniel Jorge
    Replies:
    1
    Views:
    212
Loading...

Share This Page