Wildcard with dir function

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
539
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm trying to use the dir function to get a list of files that meet a specific naming format. The format is file_name.#, where # represents a single numeric digit.

I've tried using the "#" as a wildcard but it doesn't work unless I have the syntax wrong. Any suggestions?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It's * for multiple characters and ? for a single character wildcard.
 
Upvote 0
I'm trying to use the dir function to get a list of files that meet a specific naming format. The format is file_name.#, where # represents a single numeric digit.

I've tried using the "#" as a wildcard but it doesn't work unless I have the syntax wrong. Any suggestions?

Hi KGee, I know you want to get a list of files with this naming format: file_name.#

However, what type of files you're looking for? .xlsx?

You could use this code:

Just replace the fpath with the folder you want to search in, and change the file format ".xlsx" to the one you wanna look for, e.g. ".docx" or ".pptx"

Code:
Sub getfile()
    Dim fname As String, fpath As String
 
    fpath = "C:\Users\John Smith\Desktop\Folder 1\"
    fname = Dir(fpath & "file_name.?" & ".xlsx", vbDirectory)
 
    Do While Len(fname) > 0
        Debug.Print fname
        fname = Dir
    Loop
 
End Sub
 
Upvote 0
GlennUK: I will use the "?" to limit to a single character if there is no way to further specify that the single character is a number.

antaeusguy: The files are temporary and created by one of our applications. They are named file_name.1, file_name.2, etc. There can be up to seven in total and they are not always created in sequential order.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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