List all folders and sub folders except those that begin with... Assistance requested

aarcher

New Member
Joined
Sep 5, 2015
Messages
12
Here is the code i found and adapted to my needs and works great. But I want to add an exception, to skip folders starting with ???? and move on to the next folder. Any assistance would be appreciated. So i want: For Each myfile, except.

Sub ListMyFiles(MySourcePath, includesubfolders)
Set MyObject = CreateObject("Scripting.FileSystemObject")
Set mysource = MyObject.GetFolder(MySourcePath)
On Error Resume Next
For Each myfile In mysource.Files
icol = 1
Cells(IRow, icol).Value = myfile.Name
icol = icol + 1
Cells(IRow, icol).Value = myfile.Path
icol = icol + 1
Cells(IRow, icol).Value = myfile.DateCreated
icol = icol + 1
Cells(IRow, icol).Value = myfile.datelastmodified
icol = icol + 1
Cells(IRow, icol).Value = myfile.Size
'icol = icol + 1



IRow = IRow + 1
Next
End Sub


Thanks for any assistance,

Al
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
But I want to add an exception, to skip folders starting with ????

Can you clarify what you mean by folders starting with "????", since the "?" character is a wildcard character in Windows and thus an illegal character for file and folder names.
 
Last edited:
Upvote 0
Except folders starting with "R:\zzzz" or "R:\aaaa" i do not want to list files within those folders, I have 233,000 file names to list and excluding those folders would save me about 50,000.
 
Upvote 0
Except folders starting with "R:\zzzz" or "R:\aaaa" i do not want to list files within those folders, I have 233,000 file names to list and excluding those folders would save me about 50,000.
Hi,
Add the following IF statement before loop FOR and run the FOR only if the IF stayement is FALSE
Code:
IF mysource.Name like "aaaa*" or mysource.Name like "zzzz*" then
'go next folder
Else
      'Run loop For...
      For each....
      Next
End if
 
Upvote 0
Hmm i don't seem to be getting the subfolders, i thought i was, any ideas.

Mentor beat me to it. Because your Sub ListMyFiles is coded so that it does not look at subfolders (Notice that your function parameter includesubfolders not used anywhere within the subroutine,) excluding "R:\zzzz" or "R:\aaaa" does not seem that helpful. You said you adapted this code from something else. Could it be that you took too much out?
 
Upvote 0
Hi,
I thought you had a problem with skipping particular folders naming only. To get subfolders you need to have two procedures:
1 procedure to get a main folder and its files. At the end of these procedure yiu need to call the subprocedure for the subfolders
2 procedure for the sub folders - it's pretty much the same as procedure for folders - it needs one parameter the path and within the procedure you list all the files included first and then you find another folder and call the subfolder procedure in recurrence. I can provide you with the proper code tomorrow earliestif thats ok for you.
Regards
Sebastian
 
Upvote 0
A couple of points.

1. This could take a while if you have a huge number of files and subfolders.
2. There are non-recursive ways to accomplish this as well.
3. How to list files in subfolders is a question that a million people have asked before you, so there is a lot of information available via search, both here and via google.


Code:
Sub ListMyFiles(MySourcePath)
    Dim MyObject As Object, MySource As Object, MySubFolder As Object, MyFile As Object
    Dim iRow As Long, iCol As Long
    '
    Set MyObject = CreateObject("Scripting.FileSystemObject")
    Set MySource = MyObject.GetFolder(MySourcePath)
    'On Error Resume Next

    iRow = 1
    iCol = 1
    ListFiles MyObject, MySource, iRow, iCol          'Top level
End Sub

Private Sub ListFiles(ByRef MyObject, ByRef MySource, ByRef iRow As Long, ByRef iCol As Long)
    Dim MySubFolder As Object, MyFile As Object

    On Error Resume Next
    For Each MySubFolder In MySource.subFolders
        ListFiles MyObject, MySubFolder, iRow, iCol   'recursively look in subdirectories
    Next MySubFolder

    For Each MyFile In MySource.Files
        'Except files in folders starting with "R:\zzzz" or "R:\aaaa"
        Select Case UCase(Left(MyFile.Path, 7))
        Case "R:\ZZZZ", "R:\AAAA"
        Case Else
            iCol = 1
            Cells(iRow, iCol).Value = MyFile.Name
            iCol = iCol + 1
            Cells(iRow, iCol).Value = MyFile.Path
            iCol = iCol + 1
            Cells(iRow, iCol).Value = MyFile.DateCreated
            iCol = iCol + 1
            Cells(iRow, iCol).Value = MyFile.datelastmodified
            iCol = iCol + 1
            Cells(iRow, iCol).Value = MyFile.Size
            'icol = icol + 1
            iRow = iRow + 1
        End Select
    Next MyFile
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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