For/Next instead of For Each with FileItem In SourceFolder.Files

scottcolbury

New Member
Joined
Dec 6, 2005
Messages
45
Hi all,
I need to use a For/Next loop instead of "For Each FileItem In SourceFolder.Files" when looking at files in folder.

Instead of:
For Each FileItem In SourceFolder.Files
next FileItem

I need to use:
tempFileIncrement = 1
iTotal = SourceFolder.Files.Count
For q = tempFileIncrement To iTotal
Next q

Also, if this is possible what is the syntax for retrieving the name of a file?
I imagine its something like "FileItem(q).Name"?

Any ideas?
Thanks in advance!
s_c
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
AKAIK you can't loop through the Folder.Files collection using a loop index, because the key argument in Folder.Files(key) should be a file name, not an index number. Instead, put the file names in an array using a For Each loop and then use each array element to access the Folder.Files collection.
Code:
Sub FSO_Files_Loop()

    Dim FSO As Object
    Dim SourceFolder As Object
    Dim FileItem As Object
    Dim i As Long
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder("C:\Windows")
    
    'Create array containing file names
    
    Dim FileNamesArray() As String
    ReDim FileNamesArray(1 To SourceFolder.Files.Count)
    i = 0
    For Each FileItem In SourceFolder.Files
        i = i + 1
        FileNamesArray(i) = FileItem.Name
    Next
    
    'Now loop through Files collection using each file name array element as the key value
    
    For i = 1 To SourceFolder.Files.Count
        Debug.Print i, SourceFolder.Files(CStr(FileNamesArray(i))).Name
    Next
        
End Sub
 
Upvote 0
Thanks, that worked.
Any ideas for speeding up how slow For/Each is when reading the names of files in a folder with a large number of files?
s_c
 
Upvote 0
You could run the DOS DIR command to create the array. This assumes the DIR command returns the same number of files as Files.Count.
Code:
Sub FSO_Files_Loop2()

    Dim FSO As Object
    Dim SourceFolder As Object
    Dim fileNamesArray As Variant
    Dim i As Long
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder("C:\Windows")
    
    fileNamesArray = Split(CreateObject("WScript.Shell").Exec("cmd /C DIR /B /ON /A-D """ & SourceFolder.Path).StdOut.ReadAll, vbCrLf)
    Debug.Assert UBound(fileNamesArray) = SourceFolder.Files.Count

    For i = 0 To SourceFolder.Files.Count - 1
        Debug.Print i + 1, SourceFolder.Files(CStr(fileNamesArray(i))).Name
    Next
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,102
Messages
6,170,122
Members
452,303
Latest member
c4cstore

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top