how do i open a file if i only know part of the file name

How to visible a filename*esy pattern related all files simultaneously from the directory. Please find the below code and suggest.

Const SOME_PATH As String = "C:\Anderson\"
Dim file As String
file = Dir(SOME_PATH & "filename*esy" & ".")

If (Len(file) > 0) Then
  MsgBox ("Found")
  Else
  MsgBox ("Not Found")
End If
End Sub

Open in new window

Rebecca ShabadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
It isn't clear exactly what you need. This should list the files in a series of message box displays
Const SOME_PATH As String = "C:\Anderson\"
Dim file As String

file = Dir(SOME_PATH & "filename*esy" & ".")

If (Len(file) > 0) Then
    Do While Len(file) > 0
        MsgBox "Found " & file
        file = Dir()
    Loop
  Else
    MsgBox ("Not Found")
End If

Open in new window

0
Rebecca ShabadAuthor Commented:
Need to visible multiple files at the same time but it has been partial naming structure (eq. filename*sy).

Multiple Files in Directory:

filename1292_endeesy
filename129_endeesy
filename_Aasdeesy
filename-sasks_endeesy
0
aikimarkCommented:
The period at the end of your pattern might not be filtering properly.

Will these files have an extension?
Are you trying to find files or directories or both?  

The Dir() function might not return the results you want.  It will apply the pattern against both the long name and the 8.3 name, returning more file names.

In order to get all the files, you will need to iterate through the returned file names.
Example:
Sub Q_29067534()
    Const SOME_PATH As String = "C:\Anderson\"
    Dim file As String
    file = Dir(SOME_PATH & "filename*esy" & ".")
    Do Until Len(file) = 0
        If file Like "filename*esy" & "." Then
          MsgBox ("Found: " & file)
        End If
        file = Dir
    Loop
End Sub

Open in new window

0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Rebecca ShabadAuthor Commented:
It's possible to visible all partial named(filenames*esy) xml files via notepad?
0
Bill PrewIT / Software Engineering ConsultantCommented:
Why not just do this at a DOS command prompt?

Open the Command Prompt, switch to the folder to report on via the CD command, and then run:

dir /b "filenames*esy.xml"

If you want to save the list of files to a file add:

dir /b "filenames*esy.xml">list.txt

then if you want open the list.txt file in Notepad for viewing.


»bp
0
Rebecca ShabadAuthor Commented:
Yes I know that method through Command Prompt method. So it's not possible to code in word vba?
0
Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try, it will find the matching files in the base folder, and place the list in the current word document.

Public Sub FileList()
    Const BASE_FOLDER As String = "C:\Anderson\"
    Const FILE_FILTER As String = "filename*esy.xml"

    Dim file As String
    
    file = Dir(BASE_FOLDER & FILE_FILTER)

    Selection.TypeText "Files in " & BASE_FOLDER & ":" & vbCrLf

    If (Len(file) > 0) Then
        Do While Len(file) > 0
            Selection.TypeText file & vbCrLf
            file = Dir()
        Loop
    Else
        MsgBox ("No files found")
    End If
End Sub

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rebecca ShabadAuthor Commented:
The above code is not working its only showing No files found and print C:\log: in word
0
Bill PrewIT / Software Engineering ConsultantCommented:
The two constants need to be set properly to specify a location where matching files actually exist.  There must be a backslash at the end of the BASE_FOLDER constant.

   Const BASE_FOLDER As String = "C:\Anderson\"
    Const FILE_FILTER As String = "filename*esy.xml"



»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.