Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: How to replace "For Each File In Folder.Files"?


Message #1 by ac_wolff@h... on Wed, 31 Jan 2001 11:53:52 -0000
I'm afraid you may have to take a different approach.  On p902 of Getz &
Gilbert's _Visual Basic Language Developer's Handbook_ I find the following:

    Just as mentioned earlier with the SubFolders collection, the Files
    collection looks like a normal VBA collection but its Item method only
    accepts a string as its parameter. That is, if you want to reference a
    particular file within the Files collection, you cannot do it by
    position within the collection. You must specify a filename as the key.
    If you want to iterate through all the items in the Files collection,
    you may not use a For...Next loop: instead, you must use a For
    Each...Next loop.

What a drag, eh?

-----Original Message-----
From: ac_wolff@h... [mailto:ac_wolff@h...]
Sent: Wednesday, January 31, 2001 3:54 AM
To: professional vb
Subject: [pro_vb] How to replace "For Each File In Folder.Files"?


Next code is OK in VB :

Private Sub Test()
  Dim objFileSystem As Scripting.FileSystemObject
  Dim SrcFolder As Scripting.Folder
  Dim SrcFile As Scripting.File
  
  Set objFileSystem = New Scripting.FileSystemObject
  With objFileSystem
    Set SrcFolder = .GetFolder("C:\")
    For Each SrcFile In SrcFolder.Files
      Call MsgBox(SrcFile.Name)
    Next SrcFile
  End With
End Sub

I like to translate this to Delphi, but I do not know how to 
translate the 'For Each SrcFile In SrcFolder.Files'
So I tried first in VB the next code which is easy to translate 
to Delphi:

Private Sub Test()
  Dim objFileSystem As Scripting.FileSystemObject
  Dim SrcFolder As Scripting.Folder
  Dim SrcFile As Scripting.File
  Dim Index As Long
  Set objFileSystem = New Scripting.FileSystemObject
  With objFileSystem
    Set SrcFolder = .GetFolder("C:\")
    For Index = 1 To SrcFolder.Files.Count
      Set SrcFile = SrcFolder.Files.Item(Index) 'Invalid 
procedure call or argument
      Call MsgBox(SrcFile.Name)
    Next Index
  End With
End Sub

But this code gives runtime error '5': Invalid procedure call or 
argument:

Why does this not work?

  Return to Index