Excluding specific file types from directory count

BillTony

Board Regular
Joined
May 3, 2017
Messages
70
Hi there,

I'm trying to exclude specific file types when doing a count within folders in a specific directory.

The code below works when the file is NAMED, but only when.

I need some guidance on how wildcards would be used in a situation like this.

In addition, I would GREATLY appreciate some help on excluding multiple (ex; .doc, .accdb, etc.) file types within the same routine.

Code:
Folder_Location = "some directory…"
    Set F_S_O = CreateObject("Scripting.FileSystemObject")
    Last_Row_ColA = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
    For Loop_Thru = 2 To Last_Row_ColA
 
    AcctNo_Searched = Folder_Location & Range("A" & Loop_Thru).Value
    Set Object_Files = F_S_O.getfolder(AcctNo_Searched).Files
    File_Count = Object_Files.Count
      
    For Each Ob_Ject In Object_Files
        If Ob_Ject.Name = "Thumbs.db" Then
            File_Count = File_Count - 1
        End If
        Next Ob_Ject
            Range("C" & Loop_Thru).Value = File_Count
        Next
   
End Sub

Thanks in advance!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Thanks for a awesome, and rapid solution!

I'm still having some issues with excluding multiple file types, though.

Can it be done within the SAME If/Then statement, or must it be accomplished as shown below?

Code:
If Ob_Ject.Name Like "*.db" Then
            File_Count = File_Count - 1
        End If
        If Ob_Ject.Name Like "*.docx" Then
            File_Count = File_Count - 1
        End If
    Next Ob_Ject
 
Upvote 0
You can do it like this
Code:
With Ob_Ject
    If .Name Like "*.db" Or .Name Like "*.docx" Then
        File_Count = File_Count - 1
    End If
End With
adding as many Or as you need (I'm not sure if there is a limit).
If you want to exclude all word files regardless you could change *.docx to *.doc*
 
Upvote 0
Solution
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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