Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find filename in directory to match components

Posted on 2012-03-26
3
Medium Priority
?
443 Views
Last Modified: 2012-03-27
Dear experts -
I am trying to: a) determine if a file exists in a given directory; and b) store the full name of that file (with path) to a variable.

Specifically, I want to see if there is a file in a given directory where: a) the first part of the filename; and b) end part of the filename match a given pattern (for example, in the directory: "J:\purchase orders\" is there a file that begins with the string "2001_ABC1" and ends with the string "_07.pdf".

Any thoughts are greatly appreciated!
0
Comment
Question by:terpsichore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Expert Comment

by:danishani
ID: 37769706
Try this, late binding version of using the FileSystemObject:

Private Sub Command13_Click()

Dim SourceFolderName As String

Dim fso As Object
Dim SourceFolder As Object
Dim Subfolder As Object
Dim FileItem As Object

Dim myVar As String


SourceFolderName = "C:\YourPathNameHere\"

Set fso = CreateObject("Scripting.filesystemobject")


On Error GoTo Err_Handler
   
    Set SourceFolder = fso.GetFolder(SourceFolderName)
   
    For Each FileItem In SourceFolder.Files
       If Right(FileItem.Name, 7) = "_07.pdf" And Left(FileItem.Name, 9) = "2001_ABC1" Then
       
       myVar = FileItem.Path
       Debug.Print myVar
                   
       End If
   
    Next
   
Exit_Handler:
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set fso = Nothing
   
    Exit Sub
   

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Exit_Handler


End Sub
0
 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 2000 total points
ID: 37769721
Here is a simple method using the VBA Dir() and a wildcard


Const strDir As String = "J:\purchase orders\"

Dim strFileName As String

strFileName = Dir(strDir & "2001_ABC1*_07.pdf")

Do Until strFileName = ""

   ' do some3hting with full path
   ' as test send to immediate window
   Debug.Print strDir & strFileName

   strFileName = Dir()
Loop

Open in new window

0
 

Author Closing Comment

by:terpsichore
ID: 37770948
simple, works great
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question