Dir Do Until Loop not working

Jetx227

Board Regular
Joined
Jul 11, 2018
Messages
96
Hi guys! I have this for loop iterating through each folder in a file path, then it goes into the folder and gets the names of the files. Only problem is that while it should be getting the name of every file in each folder, it only gets the name of the first file. So I though a Do Until loop would solve the issue, but it didn't do anything. Any help would be great, thanks!

Code:
For Each objFolder In objFolders
   FileName = Dir(FilePath & "\" & objFolder.Name & "\" & objFolder.Name & "*")
   Do Until FileName = ""
      'Some Commands that have to do with the filename
      FileName = Dir()
   Loop

Note, if this is normally supposed to work (because I took this off some website [codevba.com/office/loop_files_in_folder.htm] ), is it possible that commands that I use in the Do Until loop would interfere with it? Thanks
 
Last edited:
You could try
Code:
Sub looper()
    Dim objFSO As Object
    Dim objFolders As Object
    Dim objFolder As Object
    Dim objFile As Variant
    Dim strDirectory As String


    With Application.FileDialog(4)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Select Folder"
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If
        strDirectory = .SelectedItems(1)
    End With
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolders = objFSO.GetFolder(strDirectory).SubFolders
    
   For Each objFolder In objFolders
      For Each objFile In objFolder.files
         'Some Commands that have to do with the filename
         MsgBox objFile.Name
      
      Next objFile
   Next objFolder
End Sub
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You could try
Code:
Sub looper()
    Dim objFSO As Object
    Dim objFolders As Object
    Dim objFolder As Object
    Dim objFile As Variant
    Dim strDirectory As String


    With Application.FileDialog(4)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Select Folder"
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If
        strDirectory = .SelectedItems(1)
    End With
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolders = objFSO.GetFolder(strDirectory).SubFolders
    
   For Each objFolder In objFolders
      For Each objFile In objFolder.files
         'Some Commands that have to do with the filename
         MsgBox objFile.Name
      
      Next objFile
   Next objFolder
End Sub

This almost worked. It just pulled the first file as many times as there are files in the folder. (So if I have a folder with 3 files, it'll pull the first file 3 times).
 
Upvote 0
It doesn't do that for me, so not sure what is happening.
 
Upvote 0
It doesn't do that for me, so not sure what is happening.
Hi Fluff,
I just realized that when I tested your code, I forgot to change one line of my commands to call objFile and not what I had previously been using, which is what caused the issue of only pulling the first file. So your for loop works perfect! Thank you very much for your help
 
Upvote 0
Glad you figured it out & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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