determine if a file exists in a directory by its partial name

amineact

New Member
Joined
Apr 12, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello
In a production masterbook, i have 10 excel spreadsheets. They have a specific name , like File1_04052022
I'm looking to build a macro that lets me know if THE 10 files exists even if i change the dates in case of future implementations. meaning it should be able to detect File1_05052022
I set up a loop between an array containing the partial names and a loop going through the files in the directory
I tried using Instr but it seems it doesn't serve it's purpose.
Here's the code i used:
VBA Code:
Sub tester()
Dim FSO As Object
Dim fldr As Object
Dim folder As String
Dim filesnames As Variant
Dim c As Integer
folder = "Inputs taux"
filesnames = Array("File1", "File2","File3","File4","File5","File7","File9","File10")
c = 0
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fldr = FSO.GetFolder("C\Myenv\")


For Each var In filesnames
 For Each File In fldr.Files
    If InStr(File.Name, var) > 0 Then
     c = c + 1
    End If
 Next
 If c = 0 Then
  MsgBox (" There is a missing file")
  Exit Sub
 Else
 End If
Next var
If c = 10 Then
 Range("J2").Value = "OK"
End If
 


End Sub
Appreciate any help i can get
Thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try moving your If statements after both your For Each/Next loops.

Also, I would suggest that you replace . . .

VBA Code:
If c =10 Then

with

VBA Code:
If c = UBound(filesnames) + 1 Then

. . . so that you won't have worry about changing that line whenever you add and/or delete file names from the array filesnames. So maybe something like this . . .

VBA Code:
For Each Var In filesnames
    For Each file In fldr.Files
        If InStr(file.Name, Var) > 0 Then
            c = c + 1
        End If
    Next
Next Var

If c = UBound(filesnames) + 1 Then
    Range("J2").Value = "OK"
ElseIf c > 0 Then
    MsgBox "One or more files are missing"
Else
    MsgBox "No files were found"
End If

Hope this helps!
 
Upvote 0
Solution
Hello Domenic
A superb solution. I tried a solution of myslef which was non as elegant and generalized as yours.
Thanks
 
Upvote 0
That's great, I'm glad I could help.

And thanks for your feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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