Does A Variation Of A Filename Exist In A Directory

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code:

Code:
Sub cheese()
    
Stop
    sdate = 43609 'May 24 2019
    EDate = 43757 'Oct 20 2019
    Days = 148
    For lp = sdate To EDate
        t_mon = Format(lp, "mmm")
        t_day = Day(lp)
        t_dayy = Format(lp, "ddd")
        tar_str = t_mon & "-" & t_day & " (" & t_dayy & ") Schedule_*"
        srcfile = tar_str
        
        strFileName = srcpath & srcfile
        strFileExists = Dir(strFileName)
 
        If strFileExists = "" Then
            MsgBox "The selected file doesn't exist"
        Else
            MsgBox "The selected file exists"
        End If
        lp = lp + 1
    Next lp
     
End Sub

It's purpose is to access a directory (srcpath) and check whether a file (srcfile) exists within it.
Disregard the asterisk as I've placed it here only for reference purposes.
The file could be any one of three file names, the difference being in the placeholder occupied by the asterisk.

For example, the file name could be "May-24 (Fri) Schedule_3.xlsx", "May-24 (Fri) Schedule_5.xlsx" or "May-24 (Fri) Schedule_6.xlsx"
So, I need this code to recognize that if it identifies with any of these variations (will only be one variation), that the file exists.

I'm not sure what adaptation needs to be made to allow this? I could run an individual test for each variation, but I feel the task can be done more efficiently.
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Maybe you didn't know, but asterisk is a wildcard character in the Dir function, representing 0 or more characters. So your code, as posted, should determine whether any of the three variations exists.
 
Upvote 0
Thank you John. I knew the asterisk was a wildcard, but often times when I try to use it, I learn that particular function doesn't support wildcards. I just assumed this scenario was no different.
But it does work!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
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