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:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi there. Try amending you filename line like this (change in red):

FileName = Dir(FilePath & "" & objFolder.Name & "\*.*")
 
Last edited:
Upvote 0
Hi there. Try amending you filename line like this (change in red):

FileName = Dir(FilePath & "" & objFolder.Name & "\*.*")

Hi there!
I tried this and updated the code to:

Code:
For Each objFolder In objFolders
   FileName = Dir(FilePath & "" & objFolder.Name & "\*.*")
   Do Until FileName = ""
      'Some Commands that have to do with the filename
      FileName = Dir()
   Loop
This still only pulled the first file from each folder
 
Last edited:
Upvote 0
Hi there. I have plugged your code into another subroutine I use to see if it works - this code will msgbox each file in each subfolder starting from the folder you select. It shows that multiple files are selected.
Code:
Sub looper()
    Dim objFSO As Object
    Dim objFolders As Object
    Dim objFolder As Object
    Dim strDirectory As String


    With Application.FileDialog(msoFileDialogFolderPicker)
        .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
   Filename = Dir(strDirectory & "\" & objFolder.Name & "\*.*")
   Do Until Filename = ""
      'Some Commands that have to do with the filename
      Filename = Dir()
      MsgBox Filename
   Loop
   Next objFolder
End Sub
 
Upvote 0
Hi there. I have plugged your code into another subroutine I use to see if it works - this code will msgbox each file in each subfolder starting from the folder you select. It shows that multiple files are selected.
Code:
Sub looper()
    Dim objFSO As Object
    Dim objFolders As Object
    Dim objFolder As Object
    Dim strDirectory As String


    With Application.FileDialog(msoFileDialogFolderPicker)
        .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
   Filename = Dir(strDirectory & "\" & objFolder.Name & "\*.*")
   Do Until Filename = ""
      'Some Commands that have to do with the filename
      Filename = Dir()
      MsgBox Filename
   Loop
   Next objFolder
End Sub

Hi jmacleary,
So I just adjusted your code slightly and it worked perfectly, it pulled all the names of the files. Although when I tried to use my commands it is still on pulling the first file for some reason. Is it possible that they could have an affect on it? The commands essentially open a pdf, turn it into an excel workbook (which saves the workbook into that folder that the file was pulled from), copies the sheet ion the workbook that was created into a master workbook, and then deletes the workbook created from the pdf. So by the time that the loop comes back around to looking at the file location, it should be the exact same as when the loop started, which is why im not sure its having an affect on it.
 
Upvote 0
So I'm fairly certain that because I'm adding a new file and changing the dynamic of the folder (even though I delete the new file before the loop looks at the file location again), the dir function just kinda reads an error and ends the loop or something. So basically my fix is I'm just gonna try to loop through all of the files ahead of time and store the names in cells in a worksheet or something
 
Upvote 0
Pretty sure that when you first call dir it creates a "list" of files, but if the files in that folder change, then it destroys the "list" & has to rebuild it. Hence you always get the same file.
 
Upvote 0
Pretty sure that when you first call dir it creates a "list" of files, but if the files in that folder change, then it destroys the "list" & has to rebuild it. Hence you always get the same file.
Yeah, I was just trying to read into it more and I think that your explanation of what it does makes sense. I got it now so that I just use the loop that jma gave me to save the file names in a worksheet (each folder getting a column, each file in that folder getting a row) then just calling that and running my original commands. A bit of a work around, but whatever works. Thanks to everyone for the help!
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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