Consulting

Results 1 to 14 of 14

Thread: Item Property of Files Collection

  1. #1

    Item Property of Files Collection

    Hello,

    I was trying to use the item property of the Files Collection in VBA - Microsoft Scripting, in order to get the name of the file in a folder so I rename it. I have received an error and then looking more carefully I have noticed that the paramater of the Item Property is KEY...how can I retrieve this key indicator ?

    Please be aware that I managed to escape with a For Next loop and get the name of the file but I was wonderring why I can not use the Item Property...

    10x

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Use the Item and Key properties in a Dictionary object. Without seeing you code, it is hard to advise.

    Why not use the Name property?
    [vba]Function ShowFolderList(folderspec)
    Dim fso, f, f1, fc, s
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(folderspec)
    Set fc = f.Files
    For Each f1 in fc
    s = s & f1.name
    s = s & "<BR>"
    Next
    ShowFolderList = s
    End Function
    [/vba]

  3. #3
    Hello,

    I have attached the code. what i am trying to do is to rename an unarchived file (mant thanks to Ron De Bruin for his code) and the error occurs when using:
    [VBA]
    FSfolder.Files.Item(1).Name = "Sales Report.xls"
    [/VBA]

    The only way I have managed to escape it was to loop thru all the files,
    but it is not so smart as I have checked before if there is only one file in the folder...

    10x
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [VBA]Sub M_snb()
    Name "G:\OF\" & Dir("G:\OF\*.*") As "G:\OF\report.xls"
    End Sub[/VBA]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I can't see any alternative using FSO, it doesn't seem to fully act like a collection does it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    In help it is stated [VBA]Item(key) as file[/VBA]...
    But, how can be this key identified?

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    When you step through the code with F8 or enter Debug in the error, what is the value of:
    [VBA]FSfolder.Files.Item(1).Name[/VBA]

  8. #8
    it is
    Run-time error 5:
    Invalid procedure call or argument

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Ron's code is specific for your Window's operations system. What is the link to his code?

    What is you Window's operating system?

  10. #10
    it's not Ron's code, it's only that piece of syntax which does not run...
    try to set the fsfolder on a path where you have files and you will get an error.

    From the 2 properties mentioned on files collection the count works, but the item does not - since, i think the key parameter is not well expressed.

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Where in what help does it show to use it like that? I use script56.chm for fso command syntax. Keep in mind that many of the fso commands are just read-only properties. FSO methods also are very unforgiving if you try to use them under certain conditions such as the file being open already.

  12. #12
    i have attached the picture where it is stated that the parameter of Item should be key, and as XLD mentioned it does not seem to act as a collection (as for example in ranges collection the item property has an index parameter).

    anyway, thank you all for your support

  13. #13
    ...and the attachement

    Best,
    Character
    Attached Files Attached Files

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [vba]Sub tst()
    With New Scripting.FileSystemObject
    x03 = .GetFolder("G:\OF").Files("adressen.xls").Name
    End With
    End Sub[/vba]

    the key is the file's name, not it's indexnumber.

    [VBA]
    Sub M_snb()
    CreateObject("Scripting.FileSystemObject").GetFolder("G:\OF").Files("adress en.xls").Name = "000_adressen.xls"
    End Sub
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •