View Full Version : Item Property of Files Collection

02-04-2013, 08:42 AM

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...


Kenneth Hobs
02-04-2013, 08:50 AM
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?
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>"
ShowFolderList = s
End Function

02-05-2013, 01:48 AM

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:

FSfolder.Files.Item(1).Name = "Sales Report.xls"

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...


02-05-2013, 03:33 AM
Sub M_snb()
Name "G:\OF\" & Dir("G:\OF\*.*") As "G:\OF\report.xls"
End Sub

02-05-2013, 04:48 AM
I can't see any alternative using FSO, it doesn't seem to fully act like a collection does it.

02-05-2013, 05:27 AM
In help it is stated Item(key) as file...
But, how can be this key identified?

Kenneth Hobs
02-05-2013, 09:22 AM
When you step through the code with F8 or enter Debug in the error, what is the value of:

02-05-2013, 12:52 PM
it is
Run-time error 5:
Invalid procedure call or argument

Kenneth Hobs
02-05-2013, 01:33 PM
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?

02-05-2013, 02:46 PM
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.

Kenneth Hobs
02-05-2013, 08:14 PM
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.

02-06-2013, 02:59 AM
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 :)

02-06-2013, 03:01 AM
...and the attachement


02-06-2013, 06:52 AM
Sub tst()
With New Scripting.FileSystemObject
x03 = .GetFolder("G:\OF").Files("adressen.xls").Name
End With
End Sub

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

Sub M_snb()
CreateObject("Scripting.FileSystemObject").GetFolder("G:\OF").Files("adressen.xls").Name = "000_adressen.xls"
End Sub