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?
 
Bill PrewCommented:
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
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
Rebecca ShabadAuthor Commented:
It's possible to visible all partial named(filenames*esy) xml files via notepad?
0
 
Bill PrewCommented:
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
 
Rebecca ShabadAuthor Commented:
The above code is not working its only showing No files found and print C:\log: in word
0
 
Bill PrewCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.