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

Rebecca Shabad
Rebecca Shabad used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
GrahamSkanRetired
Top Expert 2012
Commented:
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

Author

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
Top Expert 2014
Commented:
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

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
It's possible to visible all partial named(filenames*esy) xml files via notepad?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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

Author

Commented:
Yes I know that method through Command Prompt method. So it's not possible to code in word vba?
Test your restores, not your backups...
Top Expert 2016
Commented:
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

Author

Commented:
The above code is not working its only showing No files found and print C:\log: in word
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial