Using Scripting.FileSystemObject via COM 
Author Message
 Using Scripting.FileSystemObject via COM

Hi

I am using a product with a COM interface that doesn't have very good
file handling so it makes sense for me to create a
Scripting.filesystemobject so I can manipulate files better.

One of the things I want to be able to do is to read a list of files
in a specific folder, so the fso.Folder.Files collection would seem
appropriate.

Unfortunately I cannot manage to iterate through these files. The
product I have [which uses a VB style language] has no way of running
a

foreach oFile in oFiles ....

The documentation shows that the Files collection has an Item
property... which also shows that this is an "index" using a "key"
(particularly for a Dictionary object), butthere is no explanation of
how to iterate through the Files Collection using the Item property.

Can anyone shed any light on this for me please as I need to be able
to build a list of files that exist in a directory.

Thanks
GWG



Fri, 02 Jul 2004 18:54:54 GMT  
 Using Scripting.FileSystemObject via COM
the Index property is directly related to the Count property of an FSO collection.
The process is essentially the same as using an 'i' counter in a For loop:

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
Path = WshShell.ExpandEnvironmentVariables("%SystemRoot%")
Set Folder = FSO.GetFolder(Path)
Set Files = Folder.Files

' now here's where the options come in
For i = 0 to Ubound (Files)
   '    do a thing with Files(i)
Next

' next option
For i = 0 to Files.Count-1
'    do a thing with Files.Index(i)
Next

--
Jim Harrison
MCP(NT4, W2K), A+, Network+, PCG
http://isaserver.org/authors/harrison/
Read the books!

Hi

I am using a product with a COM interface that doesn't have very good
file handling so it makes sense for me to create a
Scripting.filesystemobject so I can manipulate files better.

One of the things I want to be able to do is to read a list of files
in a specific folder, so the fso.Folder.Files collection would seem
appropriate.

Unfortunately I cannot manage to iterate through these files. The
product I have [which uses a VB style language] has no way of running
a

foreach oFile in oFiles ....

The documentation shows that the Files collection has an Item
property... which also shows that this is an "index" using a "key"
(particularly for a Dictionary object), butthere is no explanation of
how to iterate through the Files Collection using the Item property.

Can anyone shed any light on this for me please as I need to be able
to build a list of files that exist in a directory.

Thanks
GWG



Fri, 02 Jul 2004 23:08:47 GMT  
 Using Scripting.FileSystemObject via COM
Jim

thanks.. that is how I expected it to work.. however, even trying to run
this script within wsh I get the following error:

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:/temp")
Set oFiles = oFolder.Files

nFiles = oFiles.Count
MsgBox nFiles
For nLoop = 0 to nFiles-1
        Set oFile = oFiles.Index(nLoop)   *
        MsgBox oFile.Name
Next

The first msgbox pops up fine, telling me I have 2 files in this
directory.

Then I get the following error message at the line marked above:

800A01B6: Object does not support this property or method 'oFiles.Index'

Any further ideas?
Cheers
GWG

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Fri, 02 Jul 2004 23:57:26 GMT  
 Using Scripting.FileSystemObject via COM
Hi Gary,

I think it should be Item instead of Index.

Hope this helps,

CEF


Quote:
> Jim

> thanks.. that is how I expected it to work.. however, even trying to run
> this script within wsh I get the following error:

> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oFolder = oFSO.GetFolder("C:/temp")
> Set oFiles = oFolder.Files

> nFiles = oFiles.Count
> MsgBox nFiles
> For nLoop = 0 to nFiles-1
> Set oFile = oFiles.Index(nLoop)   *
> MsgBox oFile.Name
> Next

> The first msgbox pops up fine, telling me I have 2 files in this
> directory.

> Then I get the following error message at the line marked above:

> 800A01B6: Object does not support this property or method 'oFiles.Index'

> Any further ideas?
> Cheers
> GWG

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Sat, 03 Jul 2004 00:41:25 GMT  
 Using Scripting.FileSystemObject via COM
The Files and SubFolders collections both support indexing by a string file name index, not a numeric index.  

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:

> the Index property is directly related to the Count property of an FSO collection.
> The process is essentially the same as using an 'i' counter in a For loop:

> Set FSO = CreateObject("Scripting.FileSystemObject")
> Set WshShell = CreateObject("Wscript.Shell")
> Path = WshShell.ExpandEnvironmentVariables("%SystemRoot%")
> Set Folder = FSO.GetFolder(Path)
> Set Files = Folder.Files

> ' now here's where the options come in
> For i = 0 to Ubound (Files)
>    '    do a thing with Files(i)
> Next

> ' next option
> For i = 0 to Files.Count-1
> '    do a thing with Files.Index(i)
> Next

> --
> Jim Harrison
> MCP(NT4, W2K), A+, Network+, PCG
> http://isaserver.org/authors/harrison/
> Read the books!


> Hi

> I am using a product with a COM interface that doesn't have very good
> file handling so it makes sense for me to create a
> Scripting.filesystemobject so I can manipulate files better.

> One of the things I want to be able to do is to read a list of files
> in a specific folder, so the fso.Folder.Files collection would seem
> appropriate.

> Unfortunately I cannot manage to iterate through these files. The
> product I have [which uses a VB style language] has no way of running
> a

> foreach oFile in oFiles ....

> The documentation shows that the Files collection has an Item
> property... which also shows that this is an "index" using a "key"
> (particularly for a Dictionary object), butthere is no explanation of
> how to iterate through the Files Collection using the Item property.

> Can anyone shed any light on this for me please as I need to be able
> to build a list of files that exist in a directory.

> Thanks
> GWG



Sat, 03 Jul 2004 10:40:03 GMT  
 Using Scripting.FileSystemObject via COM
1. Ooops,yes I meant Item()... typo when composing note.

2. Michael... you say the Item() index use a string file name. I take it
you mean the name of the file is used as the index for .Item()?

So in order to be able to get the files, it is neccessary to know what
the files are called? If I don't know what the files are called, how can
I therefore build a list of files as per my initial posting?

Thanks
GWG

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sat, 03 Jul 2004 17:09:22 GMT  
 Using Scripting.FileSystemObject via COM
If the language engine you are using doesn't support COM collection enumeration (ala VBScript's "For Each") then I don't know.  I'd suggest contacting the product vendor's tech support.

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:

> 1. Ooops,yes I meant Item()... typo when composing note.

> 2. Michael... you say the Item() index use a string file name. I take it
> you mean the name of the file is used as the index for .Item()?

> So in order to be able to get the files, it is neccessary to know what
> the files are called? If I don't know what the files are called, how can
> I therefore build a list of files as per my initial posting?

> Thanks
> GWG

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 09:06:24 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Using FileSystemObject to Write to a Com port

2. FileSystemObject - How to rename a file via script?

3. Driving IE4 from a script via COM

4. Driving IE4 from a script via COM

5. Trouble Using "Scripting.FileSystemObject"

6. SortOrder when using Scripting.FileSystemObject

7. Error using Scripting.FileSystemObject

8. How to combine multiple text files in one using Scripting.FileSystemObject

9. No Authorization using Scripting.FileSystemObject in ASP

10. SortOrder when using Scripting.FileSystemObject

11. using the serial port (COM) in a VB Script

12. Question on using a com component in VB Script

 

 
Powered by phpBB® Forum Software