Looping through Folders Recursive Function

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys,

I have worked through some tutorials from WiseOwl regarding FilesAndFolders where "Andrew Gould"

It is a great video!
However if I run the recursive Function looping through all the Folders containing Say Excel Files .. within a Main Directory then after a while I get a runtime Error 70 Access denied.

My Code is not in any file where the OldFoldePath or the NewFolderPath.

VBA Code:
'From Andrew Gould WiseOwl'

Dim fso As Scripting.FileSystemObject
Dim NewFolderPath As String

Sub UsingTheScriptingRuntimeLibrary()
    
    Dim OldFolderPath As String
    
    NewFolderPath = Environ("UserProfile") & "\Desktop\NeuerOrdner"   
    OldFolderPath = "Y:\BUSINESS\PROGRAMMING"
    
    Set fso = New Scripting.FileSystemObject
    
    If fso.FolderExists(OldFolderPath) Then       
        If Not fso.FolderExists(NewFolderPath) Then
            fso.CreateFolder NewFolderPath
        End If       
        Call CopyExcelFiles(OldFolderPath)       
    End If   
    Set fso = Nothing
End Sub

Sub CopyExcelFiles(StartFolderPath As String)
    Dim fil As Scripting.File
    Dim subfol As Scripting.Folder
    Dim OldFolder As Scripting.Folder
    
    Set OldFolder = fso.GetFolder(StartFolderPath)
    
    For Each fil In OldFolder.Files
        If Left(fso.GetExtensionName(fil.Path), 4) = "xlsx" Then
            fil.Copy NewFolderPath & "\" & fil.Name
        End If
    Next fil
    
    For Each subfol In OldFolder.SubFolders
        Call CopyExcelFiles(subfol.Path)
    Next subfol
End Sub

So this Code above is located in some other directory so not in the "OldFolderPath" nor in the "NewFolderPath"

Can someone let me know what the issue could be?

Cheers

Albert
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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